0
votes

I have a recorded macro in google sheets for one sheet in my spreadsheet. I would like to edit the script so that it will apply to all sheets in the spreadsheet (so that I won't have to run the recorded macro in each sheet separately). I've seen articles on how to do this in Excel but is it possible in Google Sheets?

2
share your macroplayer0
Thanks for responding to my question although I don't quite understand your answer. I thought sharing only applies to other users. How do you share something with the other tabs/spreadsheets within your own workbook (in google sheets).grades1819

2 Answers

0
votes

are you sure it doesn't work on the other sheets? recorded macros usually use the active sheet not a named sheet.

0
votes

Although I am 9 months late in answering this, here is a working answer to your question with an example to clear all visible sheets using another function called ClearActiveSheet. If someone is still looking for how to do this, this should give an idea.

function ClearActiveSheet() {

  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getActiveSheet();

  sheet.getRange('A1').activate();  
  sheet.clear();  
};

function ClearAllVisibleSheets() {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  sheets.forEach(function(sheet) {    
    //You would need to make this the current active sheet, so that you can call the ClearActiveSheet function that works on active sheet
    sheet.activate()

    // Now since you have this sheet activated, you can call the function to clear the active sheet
    ClearActiveSheet();   
  });
};

Select the function ClearAllVisibleSheets in GoogleSpredsheets from Tools-> ScriptsEditor -> SelectFunction (ClearAllVisibleSheets) and run/or debug and you will see this do the job.

P.S: If this satisfies your need, remember to upvote and kindly accept this as an answer :).