简体   繁体   中英

Recorded Macro To Run On Multiple Tabs or All

I have the below recorded macro, I would like to run it on multiple tabs, or if easier I would like to run on all tabs.

Few named tabs are 'Richard' 'Jamie' 'Nibz'.

function Week1DP() { 
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

  spreadsheet.getRange('F8:F17').activate();

  spreadsheet.getRange('K14:L15').copyTo(spreadsheet.getActiveRange(), 

  SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION, false);
};

The main idea is that you need to iterate through all the sheets with a forEach() loop (or whatever loop you prefer):


Solution for all sheets:

You can get all the sheets with getSheets() :

function Week1DP() { 
  
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = spreadsheet.getSheets(); // get all sheets

  sheets.forEach(sh=>{                 
      sh.getRange('K14:L15').copyTo(sh.getRange('F8:F17'),
      SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION, false);            
});
};

Solution for selected sheets based on the name:

To get the sheets by name you need to use getSheetByName() :

function Week1DP() { 
  
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ["Richard","Jamie","Nibz"].map(sn=>spreadsheet.getSheetByName(sn));

  sheets.forEach(sh=>{                 
      sh.getRange('K14:L15').copyTo(sh.getRange('F8:F17'),
      SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION, false);            
});
};

Add or remove names in this array to include or exclude sheets: ["Richard","Jamie","Nibz"] .

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