0

I've been working with something similar to the bellow snipped to import data into Google Sheets via App script. Now I need to do the same for Microsoft Excel Online (365) via Office Script, but I am at a loss as I do not fully understand Office Script.

Is there an easy way to convert this Google App Script over to MS Office Script?

//Get Data

function getData() {
 
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheets = ss.getSheets();
 var datasheet = ss.getSheetByName('DATA');
 var hasCreatedHeaders = false;
 
// URL
 var url = 'https://.........';

// Create header, use customers authorization header 
 var headers = {
   'Content-Type': 'application/json',
   'Authorization': 'ABC 123.......'
 };

 var options = {
   'method': 'get',
   'headers': headers
 };
 
 var response = UrlFetchApp.fetch(url, options);
 var json = response.getContentText();
 var data = JSON.parse(json);
 var table = data.Table1; 
 datasheet.clear();
 
//for each row
 var table_new = []; // <--- create a new empty array

 table.forEach(function (row) {
    var cols = [];
    var headers = [];

    for (var prop in row.Columns) {
        if (!hasCreatedHeaders)
            headers.push(prop);

        cols.push(row.Columns[prop]);
    }

    if (!hasCreatedHeaders) {
        // datasheet.appendRow(headers);
        table_new.push(headers); // <--- add row to the array
        hasCreatedHeaders = true;
    }

    // datasheet.appendRow(cols);
    table_new.push(cols); // <--- add row to the array

});

// put the array on the sheet all at once
if(table_new.length > 0)datasheet.getRange(1,1,table_new.length,table_new[0].length).setValues(table_new);

Thank you!

I've tried different ways of writing the Office script but I've not gotten anywhere.

0 Answers0