【问题标题】:Don't Run Script on Sundays不要在周日运行脚本
【发布时间】:2018-12-05 10:12:40
【问题描述】:

我有一个脚本,它在 Google 表格中的每日触发器上运行,从一个电子表格中提取数据并将其存储在另一个电子表格中。

它运行良好,但我需要它不要在周日运行。我不太清楚如何验证日期,只在周一至周六运行脚本。

    function copyDailyreport() {
var timeStamp=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy");

var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MSM");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MSM Daily Totals");
var valuesToCopy = sheetFrom.getRange(4, 11, sheetFrom.getLastRow(), 1).getValues();

//convert the column to a row
valuesToCopy=valuesToCopy.join('*#*');
valuesToCopy=valuesToCopy.split('*#*');

//add timestamp in the first place in the row
valuesToCopy.unshift(timeStamp)

//add the row to destination sheet
sheetTo.appendRow(valuesToCopy);




var timeStamp=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy");

var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SM");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SM Daily Totals");
var valuesToCopy = sheetFrom.getRange(4, 11, sheetFrom.getLastRow(), 1).getValues();

//convert the column to a row
valuesToCopy=valuesToCopy.join('*#*');
valuesToCopy=valuesToCopy.split('*#*');

//add timestamp in the first place in the row
valuesToCopy.unshift(timeStamp)

//add the row to destination sheet
sheetTo.appendRow(valuesToCopy);



var timeStamp=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy");

var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SH");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SH Daily Totals");
var valuesToCopy = sheetFrom.getRange(4, 11, sheetFrom.getLastRow(), 1).getValues();

//convert the column to a row
valuesToCopy=valuesToCopy.join('*#*');
valuesToCopy=valuesToCopy.split('*#*');

//add timestamp in the first place in the row
valuesToCopy.unshift(timeStamp)

//add the row to destination sheet
sheetTo.appendRow(valuesToCopy);

}

谢谢!

【问题讨论】:

标签: google-apps-script


【解决方案1】:

一种方法是使用Installable Trigger。手动设置 6 个带周定时器的时间驱动触发器,周一至周六每天一个。

另一种方法是手动设置 1 个每天运行的时间驱动触发器,但请检查您的脚本是否是星期日

var currentDay = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "E");
if (currentDay !== "Sun") {
    // execute the rest of the code
}

【讨论】:

  • 谢谢安德烈亚斯!我在想一个检查/验证星期几的脚本可能是我要走的路线,我只是在执行它时遇到了一些问题。
  • 那么我的答案中的代码就足以检查是否是星期天。只需在 if 函数中插入您当前的代码,使其仅在非星期天执行
猜你喜欢
  • 2011-06-02
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
相关资源
最近更新 更多