在同一电子表格中的多个工作表上运行脚本

时间:2015-10-28 15:14:21

标签: javascript google-apps-script google-sheets

我有一个运行脚本,用当前月份更新D:N列。它目前只更新ActiveSheet,我想重构它,以便它在电子表格中的所有4张纸上运行。 enter image description here

当前代码:

function setDates(){
  var monthNames = ["Jan (Qty)", "Feb (Qty)", "Mar (Qty)",
                    "Apr (Qty)", "May (Qty)", "Jun (Qty)",
                    "Jul (Qty)", "Aug (Qty)", "Sep (Qty)",
                    "Oct (Qty)", "Nov (Qty)", "Dec (Qty)"];

  var currentMonths = SpreadsheetApp.getActiveSpreadsheet().getRange("D1:N1").getValues()[0];
  var currentMonth = new Date().getMonth();

  if (currentMonths[0] == monthNames[currentMonth]){return;}

  for (var col = 0; col < currentMonths.length; col++){
    SpreadsheetApp.getActiveSpreadsheet()
                  .getActiveSheet()
                  .getRange(1,4+col)
                  .setValue(monthNames[(currentMonth + col > 11 ?
                                        currentMonth + col - 12 :
                                        currentMonth + col)]);
  }
}

我尝试了什么:

function sheetNamesFunction(){
            var sheets = ['Sheet1',
                          'Sheet2',
                          'Sheet3',
                          'Sheet4']; 
            for (var s in sheets){
          setDates(sheets[s]);
            }
          }


 function setDates(sheetName){
  var monthNames = ["Jan (Qty)", "Feb (Qty)", "Mar (Qty)",
                    "Apr (Qty)", "May (Qty)", "Jun (Qty)",
                    "Jul (Qty)", "Aug (Qty)", "Sep (Qty)",
                    "Oct (Qty)", "Nov (Qty)", "Dec (Qty)"];

  var ss = SpreadsheetApp.openById('1Ed_x52cQx5A0RSqwxsB925wzbGt5kh0Gsi0ybl');
  var sh = ss.getSheetByName(sheetName);
  var currentMonths = sh.getRange("D1:N1").getValues()[0];

  var currentMonth = new Date().getMonth();
  if (currentMonths[0] == monthNames[currentMonth]){return;}

  for (var col = 0; col < currentMonths.length; col++){
    SpreadsheetApp.getActiveSpreadsheet()
    .getActiveSheet()
    .getRange(1,4+col)
    .setValue(monthNames[(currentMonth + col > 11 ?
                          currentMonth + col - 12 :
                          currentMonth + col)]);
  }
}

当我运行时,我得到以下内容: enter image description here

如果我运行登录

var sh = ss.getSheetByName(sheetName);
      Logger.log(sh);

我可以看到第一个值为“null”: enter image description here

1 个答案:

答案 0 :(得分:5)

如果您没有名为EXACTLY Sheet1的工作表,则需要null结果。 (你检查过这个名字吗?可能有额外的空间吗?)

这不是使用for..in构造和数组的最佳形式,而是应该使用标准的for循环。就个人而言,我发现它可以帮助我区分数组和对象(“关联数组”)。您可能需要阅读:Why is using "for...in" with array iteration a bad idea?

您可以通过稍微改变一下来避免这个问题。 您可以使用工作表对象,而不是处理工作表名称。

function setDatesOnAllSheets(){
  //  var ss = SpreadsheetApp.openById('1Ed_x52cQx5A0RSqwxsB925wzbGt5kh0Gsi0ybl');
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  for (var s=0; s<sheets.length; s++) {
     setDates(sheets[s]);
  }
}


function setDates(sheet){
  var monthNames = ["Jan (Qty)", "Feb (Qty)", "Mar (Qty)",
                    "Apr (Qty)", "May (Qty)", "Jun (Qty)",
                    "Jul (Qty)", "Aug (Qty)", "Sep (Qty)",
                    "Oct (Qty)", "Nov (Qty)", "Dec (Qty)"];

  var currentMonths = sh.getRange("D1:N1").getValues()[0];

  var currentMonth = new Date().getMonth();
  if (currentMonths[0] == monthNames[currentMonth]){return;}

  for (var col = 0; col < currentMonths.length; col++){
    sheet
    .getRange(1,4+col)
    .setValue(monthNames[(currentMonth + col > 11 ?
                          currentMonth + col - 12 :
                          currentMonth + col)]);
  }
}