Home > OS >  Something causing a huge slowdown in the execution of the DriveApp.getFileById Utilities.parseCsv
Something causing a huge slowdown in the execution of the DriveApp.getFileById Utilities.parseCsv

Time:11-16

The CSV file usually has about 10 lines with values, it is small, light but even so all executions take 200 seconds to be able to import the values from the CSV file into a spreadsheet.

What might be causing this and what should I change in the code to make it work quickly?

enter image description here

function ImportWebData() {
  const cleansheet = SpreadsheetApp.getActive().getSheetByName('ImportWebData');
  cleansheet.getRange('A1:Z'   cleansheet.getMaxRows()).clear({contentsOnly: true, skipFilteredRows: true});

  var sheet = SpreadsheetApp.getActive().getSheetByName('ImportWebData');
  var file = DriveApp.getFileById("1aYQLX-67EFJKbGy9TFnis1vjtxiro3r8");
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

Note: Drive API is active in Google Advanced Services

CodePudding user response:

I avoided the unnecessary step of clearing data that may not be there by not using getMaxRows() I just used sh.getLastRow(); I probably could have used getLastColumn() for the data but I don't know what's inside the file.

function ImportWebData() {
  const ss = SpreadsheetApp.getActive();
  const sh1 = ss.getSheetByName('ImportWebData');
  sh1.getRange(1,1,sh1.getLastRow(),26).clear({contentsOnly: true, skipFilteredRows: true});
  var file = DriveApp.getFileById("1aYQLX-67EFJKbGy9TFnis1vjtxiro3r8")
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  sh1.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

I guess you made it public and it ran in just few seconds

  • Related