Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何点击谷歌表单中的按钮弹出当前周一?_Google Apps Script_Google Sheets - Fatal编程技术网

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

我想用应用程序脚本在谷歌电子表格中创建一个按钮。请求如下所示:

  • 单击名为“刷新”的按钮
  • 弹出当前周星期一的日期,例如,今天是2021年5月7日,则单元格值应填充为2021年5月3日
  • 我知道这可以通过写剧本来实现,但我不知道如何实现

    谢谢, 喷射机

    ==========================================================================

    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);