简体   繁体   中英

adwords scripts to google spreadsheet

I have an object called dataMapEN --> var dataMapEN = {} ;

This object has say 30000 entries.
I am trying to write them to Google spreadsheet using the following code:

var sheet = ss.getSheetByName("EN");
populateSheet(sheet, dataMapEN);

function populateSheet(sheet, dataSource){
  for(item in dataSource){

    sheet.appendRow([item, dataSource[item].clicks, dataSource[item].impressions, dataSource[item].cost, dataSource[item].conversionValue, dataSource[item].conversions]);

  }
}

What I am seeing is that to write 200 entries the script takes about 2 minutes.
The script times out everytime I write to the sheet.

Is there a faster way to write to the sheet so that the script finishes in the 30min window ?

Thanks

Your script is so slow because it's doing 30k write calls to the spreadsheet.
Try putting everything into a 2d array first and then doing only one call.
Something like this (not tested because no example):

var sheet = ss.getSheetByName("EN");
populateSheet(sheet, dataMapEN);

function populateSheet(sheet, dataSource){
    var dataToWrite = dataSource.map(funtion(row, item) {
        return [item, row.clicks, row.impressions, row.cost, row.conversionValue, row.conversions];
    });

    sheet.getRange(sheet.getLastRow(), 1, dataToWrite.length, dataToWrite[0].length).setValues(dataToWrite);
}

If your data is actually an object of objects, not an array of objects iterate over Object.keys(dataSource) and return dataSource[key].clicks etc.

You may run into the danger of that being too much data to write in one go so you may want to write in bulks of 1k rows or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM