【发布时间】:2019-07-05 03:05:02
【问题描述】:
我是 Google Apps 脚本代码的新手,我正在尝试简单地将用户定义的小时数添加到当前时间(例如 12 小时)。然后,该代码将在谷歌文档中插入未来 12 小时的时间和日期。
我使用 ui.prompt 让用户输入他们想要的未来小时数。我拥有的代码没有给我一个错误,但它把当前时间放在未来一些奇怪的天数(不知道它为什么这样做)。这是我拥有的代码,else 部分是我遇到问题的地方...
function UpdateDocument() {
var document = DocumentApp.getActiveDocument();
var date = new Date();
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);
var ui = DocumentApp.getUi();
var regExpUpdateDate = "[A-Z]{3}, [A-Z]{3} [A-Z]{1}, [A-Z]{4}"; // Regular expression to find the correct line for the Next Update
// Ask User if the they want to include the Next Update Line
var response = ui.alert('Would you like to include the Next Update line?' , ui.ButtonSet.YES_NO);
// Process the users response.
if (response == ui.Button.YES) {
var responseNextUpdate = ui.prompt('Enter the number of hours you want until the next update (i.e. 12, 24, etc.)'
+ ' or leave blank if you only want to include the date and omit the time. Note that'
+ ' leaving it blank will default to the day 24 hours from now.'); // Prompts user for number of hours until the next update
if (responseNextUpdate.getResponseText() == '') {
document.replaceText(regExpUpdateDate, Utilities.formatDate(tomorrow, 'America/Denver', 'EEEE, MMMM d, yyyy'));
}
else { // This is the section that I am having issues with...
var userSelectedHours = new Date();
userSelectedHours.setDate(userSelectedHours.getHours() + 2);
document.replaceText(regExpUpdateDate, Utilities.formatDate(userSelectedHours, 'America/Denver', 'h a EEEE, MMMM d, yyyy'));
}
}
}
【问题讨论】: