Home > other >  How do I run Loop correctly in Google Apps Script
How do I run Loop correctly in Google Apps Script

Time:08-11

I`m trying to copy and paste an array from a sheet called "home" to another called "db". For some reason, the last row it is not set correctly in db sheet. See the code below:

function myFunction() {
  var home = SpreadsheetApp.getActive().getSheets()[0];
  var db = SpreadsheetApp.getActive().getSheetByName("db");
  var lastrowhome = home.getRange('C10:C100').getLastRow();
  var lastrowdb = db.getLastRow();

  for (var i = 10; i <= lastrowhome; i  ){
    var copy = home.getRange(i, 3);
    var paste = db.getRange(lastrowdb 1,3);
    paste.setValues(copy.getValues());
     }
   }

Furthermore, the code demands too much time to finish

thanks!

CodePudding user response:

Copying data from one sheet to another

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const home = ss.getSheetByName("home");
  const db = ss.getSheetByName("db");
  const sr = 10
  const vs = home.getRange(sr, 1, sh.getLastRow() - sr   1, sh.getLastColumn()).getValues();
  db.getRange(sr,1,vs.length,vs[0].length).setValues(vs);
}
  • Related