0
votes

Can Google Apps Script copy the values from on cell and paste the values onto another cell in the same sheet, and then repeat for all sheets? I'm trying to create a script whereby the value in A1, for each Sheet gets copied (value only) onto B1. This function only works in one sheet:

    function CopyToRange() {
      var source = SpreadsheetApp.getActiveSpreadsheet();
      source.getRange('A1').copyTo(source.getRange('B1'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
    }
1

1 Answers

1
votes

Explanation:

  • Firstly, you need to get an array of all the sheet objects. You can achieve that by using getSheets().

  • Then you need to iterate over every sheet and one way to do that is by using a forEach loop.


Solution:

function CopyToRange() {
      const source = SpreadsheetApp.getActiveSpreadsheet();
      const sheets = source.getSheets();
      sheets.forEach(sh=>{
      sh.getRange('A1').copyTo(sh.getRange('B1'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
      });
 }