How can I run a google script automatically on multiple sheets in the same spreadsheet

前端 未结 1 1603
暗喜
暗喜 2021-01-24 21:47

I have the following script.

Situation:

I have a spreadsheet with 10 worksheets and 15 users logging in and modifying it.

Script Function:

相关标签:
1条回答
  • 2021-01-24 22:03

    There is no active sheet when your function runs as a trigger. The active sheet is applicable only when a user has the spreadsheet open. Modify your script to use getSheetByName and everything should work fine.

    If you want this to work on multiple sheets, just call this function from another function..

    function shellFunction(){
      var sheets = ['sheet1','sheet2',etc]; 
      for (var s in sheets){
        toTrigger(sheets[s]);
      }
    }
    

    And change your toTrigger function to receive the sheet name as argument.

    function toTrigger(sheetName){
      var sh = SpreadsheetApp.openById(ID OF SPREADSHEET).getSheetByName(sheetName);
      /* Your regular code */
    }
    
    0 讨论(0)
提交回复
热议问题