Profile picture for user njadas

Hi all,

I am writing a script to maintain historical data which I need to use in a dashboard (e.g. active users over a period of time). I've managed to build the code for generating the daily data and storing the data in an excel sheet in ADS. My issue now is to expand the script to (A) read the existing excel sheet from ADS, (B) append the new data to existing sheet (daily) and then (C) store the sheet again in ADS. Can anybody help please with steps A & B. I can't seem to find a way to do that.

Thanks. 

 

 

by Freddy Barkhuizen
Posted on Fri, 03/15/2024 - 11:12

Hi Nicola,

Create a folder called "Test" in your ADS and import the attached spreadsheet called "test.xlsx".

Import the attached script and run it from any DB context.

The script will read the spreadsheet from ADS, insert a new row with incrementing number and write back the updated spreadsheet to ADS.

var sADSFolder = "Test";
var sADSFile = "test.xlsx";

var cADS = Context.getComponent("ADS");

var repADS = cADS.getADSRepository("portal");

var fldADS = repADS.getFolder(sADSFolder);

var docExcel = repADS.getDocument(fldADS, null, sADSFile);

var isExcel = docExcel.getDocumentContent();

var baExcel = Packages.org.apache.commons.io.IOUtils.toByteArray(isExcel);

var wbExcel = Context.createExcelWorkbook(sADSFile, baExcel);

var shtExcel = wbExcel.getSheetAt(0);

var nLastRowNo = shtExcel.getLastFilledRowNum();

var cellUpdate = shtExcel.cell(nLastRowNo+1,0);

cellUpdate.setCellValue(Number(shtExcel.getCell(nLastRowNo,0).getCellValue())+1);

wbExcel.write();

var fExcel = Context.getFile(sADSFile, Constants.LOCATION_OUTPUT);

var metaDoc = repADS.createDocumentMetaInfo("", sADSFile, "");

docExcel = repADS.createAndOverwriteExistingDocument(fldADS, metaDoc, new Packages.java.io.ByteArrayInputStream(fExcel)); 

if(docExcel!=null){
    Dialogs.MsgBox("Updated");
}

Regards,

Freddy

1

Featured achievement

Rookie
Say hello to the ARIS Community! Personalize your community experience by following forums or tags, liking a post or uploading a profile picture.
Recent Unlocks

Leaderboard

|
icon-arrow-down icon-arrow-cerulean-left icon-arrow-cerulean-right icon-arrow-down icon-arrow-left icon-arrow-right icon-arrow icon-back icon-close icon-comments icon-correct-answer icon-tick icon-download icon-facebook icon-flag icon-google-plus icon-hamburger icon-in icon-info icon-instagram icon-login-true icon-login icon-mail-notification icon-mail icon-mortarboard icon-newsletter icon-notification icon-pinterest icon-plus icon-rss icon-search icon-share icon-shield icon-snapchat icon-star icon-tutorials icon-twitter icon-universities icon-videos icon-views icon-whatsapp icon-xing icon-youtube icon-jobs icon-heart icon-heart2 aris-express bpm-glossary help-intro help-design Process_Mining_Icon help-publishing help-administration help-dashboarding help-archive help-risk icon-knowledge icon-question icon-events icon-message icon-more icon-pencil forum-icon icon-lock