1
votes

I am loading a list into a google sheet tab SheetA then amending the data cell by cell into the same google sheet in a different tab SheetB with some manipulation as it goes over.

But the only way I know how to do it is activate the sheet each time and its really slow. Is there a way to move the data without having to physically activate the sheet. So for eaxmple grap the value of cell A1 from SheetA without ever deactivating SheetB?

Here is a sample of the section of code where you can see it activating the sheets back and forth. I used to do a similar function in Excel but in Excel I didn't have to activate the sheets and I could turn off the visual during runtime which made the whole transfer quite fast.

Can you do the same in google sheets? If so what is the syntax?

   while (SpreadsheetApp.getActiveSheet().getRange('A'+ COUNTERSheetA).getValue() != ""){
     VALUEA = SpreadsheetApp.getActiveSheet().getRange('A'+ COUNTERSheetA).getValue()
     VALUEB = SpreadsheetApp.getActiveSheet().getRange('B'+ COUNTERA).getValue()
     spreadsheet.setActiveSheet(spreadsheet.getSheetByName('SheetA'), true);
     spreadsheet.getRange('A'+COUNTERSheetB).activate();
     spreadsheet.getCurrentCell().setValue(VALUEA);
     spreadsheet.getRange('B'+COUNTERSheetB).activate();
     spreadsheet.getCurrentCell().setValue(VALUEB);
     COUNTERSheetB = COUNTERSheetB + 1
     COUNTERSheetA = COUNTERSheetA + 1
     spreadsheet.setActiveSheet(spreadsheet.getSheetByName('SheetA'), true);
   }
2
Now you can upvote any question/answer. Welcome to stackoverflowDevLoverUmar

2 Answers

1
votes

You sure can. You can simply declare each sheet as a variable, then use the sheet class to make the calls. Example below. Take into account that each time you use the getRange() method it's taxing on the runtime. A better way to do this would be to take all values at once into a 2D array, then iterate over the array, converting into a new 2D array, then write that 2D array to the other sheet.

function myFunction() {
 let COUNTERSheetA = 1;
 let COUNTERSheetB = 1;
 let VALUEA;
 let VALUEB;
 const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 const sheetA = spreadsheet.getSheetByName('SheetA');
 const sheetB = spreadsheet.getSheetByName('SheetB');
 
  while (sheetA.getRange('A'+ COUNTERSheetA).getValue() != ""){ 
  VALUEA = sheetA.getRange('A'+ COUNTERSheetA).getValue() 
  VALUEB = sheetA.getRange('B'+ COUNTERSheetA).getValue() 
  sheetB.getRange('A'+COUNTERSheetB).setValue(VALUEA); 
  sheetB.getRange('B'+COUNTERSheetB).setValue(VALUEB);
  COUNTERSheetB = COUNTERSheetB + 1 
  COUNTERSheetA = COUNTERSheetA + 1 
  }
}

Here is the actual sheet in case you want to play with it: https://docs.google.com/spreadsheets/d/1Gev2KxpX5mPik92Io3eOeF3nKngVcKxnWiNktlHxdDI/edit?usp=sharing

Here is an example of pulling all values, iterating over them, then inserting them into the new sheet. If you can figure out the logic you need here, this is by far the fastest method.

function myFunction() {
 let VALUES;
 const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 const sheetA = spreadsheet.getSheetByName('SheetA');
 const sheetB = spreadsheet.getSheetByName('SheetB');
 
  VALUES = sheetA.getRange(1, 1,sheetA.getLastRow(), 2).getValues();
  
  for (let row of VALUES){
    for (let val of row){
    //Do your manipulations here if you can.
      Logger.log(val);
    }
  }
  sheetB.getRange(1, 1,sheetA.getLastRow(), 2).setValues(VALUES);
}

Here is the sheet for the above code: https://docs.google.com/spreadsheets/d/1Gev2KxpX5mPik92Io3eOeF3nKngVcKxnWiNktlHxdDI/edit?usp=sharing

2
votes
function myfunc() {
  const ss=SpreadsheetApp.getActive();
  const shA=ss.getSheetByName('SheetA');//gets SheetA by name
  const rgA=shA.getDataRange();//gets all of the data in the sheet
  var vA=rgA.getValues();//gets all the data at one time as one two dimensional array of data
  vA.forEach(function(r,i){
    r.forEach(function(c,j){
      vA[i][j]=(WhateverYouWanttoDoToEachCell);
    });
  });
  const shB=ss.getSheetByName('SheetB');//gets SheetB by name
  const rgB=shB.getRange(1,1,vA.length,vA[0].length).setValues(vA);//1,1 is A1 but you could choose the upper left corner to be anywhere.  Loads all of the data at one time.
  //If you're going to be doing something with those spreadsheet values immediately you may wish to employ SpreadsheetApp.flush() to guarantee that all of the values have been loaded into the spreadsheet.
  SpreadsheetApp.flush();
}

Array.forEach() method

Spreadsheet Reference