Google apps script 如何点击谷歌表单中的按钮弹出当前周一?
我想用应用程序脚本在谷歌电子表格中创建一个按钮。请求如下所示:Google apps script 如何点击谷歌表单中的按钮弹出当前周一?,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我想用应用程序脚本在谷歌电子表格中创建一个按钮。请求如下所示: 单击名为“刷新”的按钮 弹出当前周星期一的日期,例如,今天是2021年5月7日,则单元格值应填充为2021年5月3日 我知道这可以通过写剧本来实现,但我不知道如何实现 谢谢, 喷射机 ========================================================================== function popMonday() { var sheet = SpreadsheetApp.g
function popMonday() {
var sheet = SpreadsheetApp.getActiveSheet();
var cell = SpreadsheetApp.getActiveSpreadsheet().getSheets()[4].getRange('L1');
SpreadsheetApp.setCurrentCell(cell);
var selection = SpreadsheetApp.getSelection();
var currentCell = selection.getCurrentCell();
const today = new Date();
const tw = today.getDay();
const dateObj = new Date(today.setDate(today.getDate() - (tw - (tw > 0 ? 1 : -6))));
const str = Utilities.formatDate(dateObj, Session.getScriptTimeZone(), "MM/dd/yyyy");
currentCell.setValue(str);}
我相信你的目标如下
- 您希望使用Google Apps脚本从今天开始检索本周一的日期
- 您希望打开一个包含检索日期的对话框
- 在本例中,我认为使用
检索一周中的某一天很重要getDay()
- 运行函数
时,会打开一个对话框。您可以在对话框中看到日期myFunction
- 从
开始,当您使用以下脚本而不是
代码>,当脚本运行时,将打开一个对话框,并且将Browser.msgBox(str)时,单元格值应填充5/3/2021。
的值放入活动单元格dateObj
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(str), "sample"); SpreadsheetApp.getActiveRange().setValue(dateObj);
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(str), "sample");
SpreadsheetApp.getActiveRange().setValue(dateObj);