Jampong
Jampong

Reputation: 25

Office Script to format raw data and export to csv

I am new with Office Scripts. Here is what I am trying to accomplish

This is raw data

This is output

Here is Raw_Data sheet

A B C D E F G H I J K L
1 RECTYPE ORDUNIQ CUSTOMER SHIPTO TYPE ORDDATE EXPDATE PONUMBER LOCATION REQUESDATE
2 RECTYPE ORDUNIQ LINENUM LINETYPE ITEM _MISCCHARG _PRICELIST _LOCATION QTYORDERED _PRIUNTPRC _ORDUNIT _EXTINVMIS
3 RECTYPE ORDUNIQ LINENUM SERIALNUMF DETAILNUM MOVED QTY QTYMOVED
4 RECTYPE ORDUNIQ LINENUM LOTNUMF DETAILNUM EXPIRYDATE STKQTY QTY STKQTYMOVE QTYMOVED
5 RECTYPE ORDUNIQ PAYMENT DISCBASE DISCDATE DISCPER DISCAMT DUEBASE DUEDATE DUEPER DUEAMT
6 RECTYPE ORDUNIQ UNIQUIFIER DETAILNUM COINTYPE COIN
7 RECTYPE ORDUNIQ OPTFIELD VALUE
8 RECTYPE ORDUNIQ LINENUM OPTFIELD
9 1 1 WAFHOM 88888 1 20240610 20240801 2462460 USA 20240806
10 2 1 1 1 ANT1 2 0
11 2 1 1 1 ANT2 4 0
12 2 1 1 1 ANT3 1 0
13 1 1 WAFHOM 999999 1 20240610 20240801 2462460 CAN 20240806
14 2 1 1 1 PINA1 4 0

Here is the output sheet

A B C D E F G
1 20240610 20240801 2462460 USA 20240806 ANT1 2
2 20240610 20240801 2462460 USA 20240807 ANT2 4
3 20240610 20240801 2462460 USA 20240808 ANT3 1
4 20240610 20240801 2462460 CAN 20240806 PINA1 4

Here is what I've tried to come up with which is not much

function main(workbook: ExcelScript.Workbook) {
    let test_row = 9;
    let test_column = 1;
    let test_empty = 0;
    
    const sheet_temp = workbook.getWorksheet("Temp");
    const sheet_raw = workbook.getWorksheet("Raw_Data");
    const sheet_Upload = workbook.getWorksheet("Upload_SO_Xorosoft");

    while (test_empty == 0) {
        if (sheet_temp.getCell(test_row, test_column).getValue() == 1) {
            //this to test if A9=1
            ;
        }
        else if (sheet_temp.getCell(test_row, test_column).getValue() == 2) {
            //this to test if A9=2
            ;
        }
        else {
            //this to test if A9 is blank
            //complete do while condition
            test_empty=1;
        }
         //export to csv
    }
}

Upvotes: 0

Views: 412

Answers (1)

taller
taller

Reputation: 18778

The data extraction logic can be simplified as follows:

  • If a cell in Column A is 1, then retrieve 5 columns (Columns A to E in the output table).
  • If a cell in Column B is 2, then retrieve 2 columns (Columns F and G in the output table).

  • Load data into array dataValues
  • Populate output array resRows
  • Write output to sheet
function main(workbook: ExcelScript.Workbook) {
    const startRow = 9;
    const colCnt = 10;
    const sheet_temp = workbook.getWorksheet("Temp");
    const sheet_raw = workbook.getWorksheet("Raw_Data");
    const lastRow = sheet_raw.getRange("A:A").getLastCell().getRangeEdge(ExcelScript.KeyboardDirection.up).getRowIndex();
    const dataValues = sheet_raw.getRangeByIndexes(startRow - 1, 0, lastRow - startRow + 2, colCnt).getValues();
    let resRows: [] = [];
    let dataRow: [] = [];
    for (let i = 0; i < dataValues.length; i++) {
        if (dataValues[i][0] == "1") {
            dataRow = [];
            for (let j = 5; j < dataValues[0].length; j++) {
                dataRow.push(dataValues[i][j]);
            }
        }
        else if (dataValues[i][0] == "2") {
            let newRow: [] = [...dataRow];
            newRow.push(dataValues[i][4]);
            newRow.push(dataValues[i][8]);
            // console.log(newRow)
            resRows.push(newRow)
        }
    }
    let useRange = sheet_temp.getUsedRange();
    if(useRange){ useRange.clear(ExcelScript.ClearApplyTo.all);}
    sheet_temp.getRangeByIndexes(0, 0, resRows.length, resRows[0].length).setValues(resRows);
}

Note:

  • OfficeScripts cannot export a sheet to CSV like VBA can. However, you can accomplish this using Power Automate.

Save XLSX Excel all worksheets as csv

Upvotes: 0

Related Questions