简体   繁体   中英

Memory limit exceeded in AppsScript Google Sheets bound function

Have you ever met the cases when the owner of Google Sheets doesn't have the "Exceeded memory limit" error when running bound script, and the editor of the same document does?

I have a report in Google Sheets which is created as the result of API request to Google BigQuery. Here's my bound script (I excluded the full SQLquery text, because it's not the issue):

function Report_detailed() {
   var t = new Array(0);
  var projectId = 'project_id';
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Detailed_report");
  var result = sheet; 
  var dateFrom =  Utilities.formatDate(new Date(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Detailed_report").getRange("B1:B1").getValue()),"GMT+3", "yyyy-MM-dd");
  var dateTo =  Utilities.formatDate(new Date(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Detailed_report").getRange("B2:B2").getValue()),"GMT+3", "yyyy-MM-dd");
  var lastRow = sheet.getLastRow();
  sheet.getRange(6,1,lastRow,55).clearContent(); 
  var cell = sheet.getRange("G6:BC");
  cell.setNumberFormat("0.00");
  var request = {
        query: 
'                SELECT * '+
'                FROM '+
'                `project_id.dataset_id.table_name` '+ 
'                WHERE '+    
'                date  BETWEEN "'+dateFrom+'" AND "'+dateTo+'" ',    
        useLegacySql: "FALSE",
  } ;
  t[0] = new Date();
  t[0] = 'Stage 1'+t[0];
  var queryResults = BigQuery.Jobs.query(request, projectId);
  t[1] = new Date();
  t[1] = 'Stage 2'+t[1];
  var jobId = queryResults.jobReference.jobId;

    // Check on status of the Query Job.
  var sleepTimeMs = 1000;
  var n = 0;
  while (!queryResults.jobComplete) {
    n = n + 1;
    Utilities.sleep(sleepTimeMs);
    sleepTimeMs *= 2;
    queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId);
  }
  t[2] = new Date();
  t[2] = 'Stage 3'+t[2];
  Logger.log ('jobs');

  // Get all the rows of results.
  var rows = queryResults.rows;
  while (queryResults.pageToken) {
    queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId, {
      pageToken: queryResults.pageToken,
    });
    rows = rows.concat(queryResults.rows);
  }
  t[3] = new Date();
  t[3] = 'Stage 4'+t[3];
  if (rows) {

    // Append the results.
    var data = new Array(rows.length);
    for (var i = 0; i < rows.length; i++) {
      var cols = rows[i].f;
      data[i] = new Array(cols.length);
      for (var j = 0; j < cols.length; j++) {
        data[i][j] = cols[j].v;
      }
    }
    t[4] = new Date();
    t[4] = 'Stage 5'+t[4];
      Logger.log ('datas');

    sheet.getRange(6, 1, rows.length, 55).setValues(data);
    t[5] = new Date();
    t[5] = 'Stage 6'+t[5];    
    Logger.log(t.join('\\n'));

    Logger.log('Results spreadsheet created: %s');
  } else {
    Logger.log('No rows returned.');
  }
  return n;
}

Additional info:

  1. The function is called from UI.
  2. The query size is about 100 MB, so it's performed very fast.
  3. The first 4 stages are run in 1-2 minutes
  4. The result table has about 25 000 rows. So the longest part of the process is setting values to the cells.
  5. The Sheets locale is the US, the owner is in Ukraine, the editors of the file are in the other country.

When I run the function from my Google Account, everyting's OK, and I get the result table in 5-6 minutes.

But when my colleagues from the other country try to refresh the report in their Google Accounts, applying the same date range as I do, they have "Memory limit exceeded" without any detailed explanations.

Could you please help me to find the roots of the issue?

I'd appreciate any clues and recommendations. Thank you so much!

Troubleshooting

Check for file access

Do all your users have access to the data you are trying to retrieve?

Encapsulate your code with try catch

If your code is failing due to a Query issue, encapsulate it with try catch and Log the errors.

Monitor the execution and determine where it breaks

Use the Apps Script Debugger to determine in which step your code fails and what the variables look like when it does.

Check Quotas and Limits

Check the quotas and limits and determine if you have exceeded them. Error messages will point you to this by saying that you were timed out or have to wait before trying again.

Script Execution timeout

Apps script also has their own quotas , make sure your code can be executed in the time-frame determined. Specially when dealing with Data In/Out, it's important to use Batch operations.

If all else fails...

Contact G Suite Support for your domain.

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