title: How can I run a google script automatically on multiple sheets in the same spreadsheet
body: <p>I have the following <a href="https://stackoverflow.com/questions/16600972/insert-the-result-count-number-of-days-between-two-days-in-spreadsheet-using-goo">script</a>.</p>
  
  <h1>Situation:</h1>
  
  <p>I have a spreadsheet with 10 worksheets and 15 users logging in and modifying it.</p>
  
  <h1>Script Function:</h1>
  
  <blockquote>
    <p>Inserts the number of days between two days in the activesheet in a specific column,
    Script compare date from column J and TodayDay and inserts the difference in the column F.</p>
  </blockquote>
  
  <h1>Problem:</h1>
  
  <blockquote>
    <p>I can not run this script with using timed triggers. I want to run this script every 15 min for the concurrent sheet.</p>
  </blockquote>
  
  <h1>Test Case:</h1>
  
  <blockquote>
    <p>The trigger only runs with timing if I have only one sheet in the spreadsheet.
    The trigger doesn't run with timing if I have more than one sheet.
    The trigger only runs for more than one sheet if I set the same trigger OnEdit.</p>
  </blockquote>
  
  <p>I need to run this script for all sheets or active sheet every 15mins because I have two other scripts running OnEdit. When I set this script also in OnEdit the spreadsheet turn slowly.</p>
  
  <pre><code>    function onOpen() {
        var menuEntries = [ {name: "UpdateAge", functionName: "toTrigger"},
                           ];
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        ss.addMenu("Tools",menuEntries);//
      }
  
      // create a timer trigger that will call "toTrigger" every 15 minutes
  
      function toTrigger(){
        var sh = SpreadsheetApp.getActiveSheet();
        var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
        for(var n=0;n&lt;data.length;++n){
        if(typeof(data[n][9])=='object'){
          data[n][5]=dayToToday(data[n][9])
            }
          }
         sh.getRange(1,1,data.length,data[0].length).setValues(data) 
      }
  
      function dayToToday(x){
        var refcell = x;;// get value in column A to get the reference date
        var refTime = new Date(refcell);
        var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
        var today = new Date();
        var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
        var day = parseInt(TD-ref);// get the difference in days (integer value )
        return day ; // return result that will be in cell
      }
  </code></pre>
  
  <h1>EDIT: WORKING</h1>
  
  <p>Hi Srik,</p>
  
  <p>Thanks so much Now is Working.</p>
  
  <pre><code>    function onOpen() {
        var menuEntries = [ {name: "UpdateAge", functionName: "shellFunction"},
                           ];
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        ss.addMenu("Tools",menuEntries);//
      }
  
          function shellFunction(){
          var sheets = ['Sheet1','Sheet2','Sheet3','Sheet4']; 
          for (var s in sheets){
        toTrigger(sheets[s]);
          }
        }
  
        // create a timer trigger that will call "toTrigger" every 15 minutes
  
        function toTrigger(sheetName){
        var ss = SpreadsheetApp.openById('SHEET ID');
        var sh = ss.getSheetByName(sheetName);
        var data = sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
        for(var n=0;n&lt;data.length;++n){
        if(typeof(data[n][9])=='object'){
          data[n][5]=dayToToday(data[n][9])
            }
          }
         sh.getRange(1,1,data.length,data[0].length).setValues(data) 
      }
  
      function dayToToday(x){
        var refcell = x;;// get value in column A to get the reference date
        var refTime = new Date(refcell);
        var ref = refTime.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
        var today = new Date();
        var TD = today.setHours(0,0,0,0)/(24*3600000);// set hours, minutes, seconds and milliseconds to 0 if necessary and get number of days
        var day = parseInt(TD-ref);// get the difference in days (integer value )
        return day ; // return result that will be in cell
      }
  </code></pre>