How to select spreadsheets one after another in Google Apps Script to perform some action in them?
In excel vba the following code worked, but it does not work in Google Apps Script
Dim wb as Workbook
Dim ws as Worksheet
Set wb = ActiveWorkbook
For Each ws in wb.Worksheets
'Do something here
End if
Next
Could anyone help me with that?
CodePudding user response:
- You get the active spreadsheet (workbook) with SpreadsheetApp.getActiveSpreadsheet()
- You get the list of sheets (worksheets) in your spreadsheet with Spreadsheet.getSheets().
- There are many ways to iterate through the sheets. For example, with forEach.
function myFunction() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheets = spreadsheet.getSheets();
sheets.forEach(sheet => {
// Do something
});
}
Note:
- I strongly recommend taking a look at Apps Script guides. There's a good list of links here, where you can start learning.
