Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
How to optimize this function for large dataset (100k rows & 10 cols) using Google Apps Script?

This function runs ok, but for up to 20k rows. What could I change to make it run fast?

const SOURCE_FILE_ID = 'ID';

function getData() {
  const sourceSheet = SpreadsheetApp.openById(SOURCE_FILE_ID);
  const sourceRng = sourceSheet.getSheetByName('ativcopiar').getRange(1, 1, sourceSheet.getLastRow(), 9);
  const sourceValues  = sourceRng.getValues();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Ativ.');
  var destinationRng = sheet.getRange(1, 1, sheet.getLastRow(), 9);
  destinationRng.clearContent();

  destinationRng.setValues(sourceValues);
}

Sometimes, it exceeds the time limit.

Appreciate any light! Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe your goal is as follows.

  • You want to reduce the process cost of your script.

In this case, how about using Sheets API? When Sheets API is used, the process cost can be reduced a little. Ref When Sheets API is used for your script, it becomes as follows.

Modified script:

Before you use this script, please enable Sheets API at Advanced Google services.

function myFunction() {
  const SOURCE_FILE_ID = '###';
  const sheet = SpreadsheetApp.openById(SOURCE_FILE_ID).getSheetByName('ativcopiar');
  const dstSS = SpreadsheetApp.getActiveSpreadsheet();
  var dstSheet = dstSS.getSheetByName('Ativ.');
  var destinationRng = dstSheet.getRange(1, 1, dstSheet.getLastRow(), 9);
  destinationRng.clearContent();
  SpreadsheetApp.flush();
  const values = Sheets.Spreadsheets.Values.get(dstSS.getId(), "'ativcopiar'!A1:I" + sheet.getLastRow()).values;
  Sheets.Spreadsheets.Values.update({values}, dstSS.getId(), "Ativ.", {valueInputOption: "USER_ENTERED"});
}

References:

  • Benchmark: Reading and Writing Spreadsheet using Google Apps Script
  • Method: spreadsheets.values.get
  • Method: spreadsheets.values.update
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!