【问题标题】:Google sheet + App Script + HTML Stop Element from clearing after onclick function in google sheet sidebar谷歌工作表+应用脚本+ HTML停止元素在谷歌工作表侧边栏中的onclick功能后清除
【发布时间】:2021-06-02 20:20:50
【问题描述】:

您好,我正在使用我的 google 表格上的侧边栏来选择开始日期和结束日期点击按钮后选择的日期保持不变,这是我的 gs 代码

    function StartDate(numstart){
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const ws = ss.getSheetByName('DateFromTo').getRange('A2'); 
      ws.setValue(numstart)
    }

  function EndDate(numend){
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const ws = ss.getSheetByName('DateFromTo').getRange('B2');
    ws.setValue(numend)
  }

这是我的 html 代码的一部分,我尝试添加“return false”,但没有成功

<h4 class="text-left">From Date</h4>
<input type="date" class="form-control" id="startDate" name ="startDate" required>
        <h4 class="text-left">To Date</h4>
<input type="date" class="form-control" id="endDate" name ="endDate" required>
        </div>


<div class="form-group"> 
        <button  onclick="StartDate();EndDate(); return false;" type="button" class="btn btn-success col-md-2 btn-lg">Set Date</button>
    </div>



    <script>
        function StartDate(){
        var startdate = document.getElementById("startDate");
        var numstart = startdate.value
        google.script.run.StartDate(numstart);
        document.getElementById("startDate").value=("")
        
        }
    </script>


    <script>
        function EndDate(){
        var startdate = document.getElementById("endDate");
        var numend = startdate.value
        google.script.run.EndDate(numend);
        document.getElementById("endDate").value=("")
        }
    </script>

谢谢

【问题讨论】:

  • 很遗憾,我无法复制您在what I'm having issue with is I would like for the selected date to remain and not be cleared after the button is clicked 的情况。我为此道歉。我以为你的document.getElementById("startDate").value=("") 和document.getElementById("endDate").value=("") 可以用来清除它。那我能问一下你的情况吗? 1、你用的是什么浏览器? 2. 您能否提供一个示例电子表格来复制您的问题,包括您当前的脚本?
  • 您好 Tanaike 感谢您的回复,这是我为测试我的问题而创建的测试表的链接docs.google.com/spreadsheets/d/… 再次感谢
  • 我用的是Chrome浏览器
  • 感谢您回复并添加更多信息。根据您的回复,我提出了一个修改后的脚本作为答案。你能确认一下吗?

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


【解决方案1】:

修改点:

  • 起初,我以为您想在单击按钮时清除输入标签。当我看到您的示例电子表格时,我看到了 I need those 2 field above to retain there selected values after clicking the post button 和 what im trying to get is once I selected the From Date and the To Date from this sidebar date picker then click on Post Date button I want the selected date to remain or be reposted in the From Date and To date feild of this Google sidebar instead of being cleared。在这种情况下,我了解到当您选择 From Date 和 To Date 的日期并单击按钮 Post Date 时,您希望保留所选日期。而且,当您单击按钮 Clear/Cancel 时,您想清除日期。

当以上几点反映到你的脚本中时,它变成如下。

修改脚本:

请在Javascript端修改StartDate()和EndDate()如下。

function StartDate(){
  var startdate = document.getElementById("startDate");
  var numstart = startdate.value
  google.script.run.StartDate(numstart);
}

function EndDate(){
  var startdate = document.getElementById("endDate");
  var numend = startdate.value
  google.script.run.EndDate(numend);
}

并且,对于您的问题中未显示的&lt;button onclick="Clear()" type="button" class="btn btn-warning col-md-2 btn-lg"&gt;Clear / Cancel&lt;/button&gt;,请在Javascript端准备函数Clear(),如下所示。

function Clear() {
  document.getElementById("startDate").value = "";
  document.getElementById("endDate").value = "";
}
  • 通过以上修改,当您选择From Date和To Date的日期并单击按钮Post Date时,选择的日期将被保留。而且,当您单击按钮 Clear/Cancel 时,所选日期将被清除。

【讨论】:

  • 再次感谢 Tanaike 这完全符合我的需要
  • @Yvan L 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
猜你喜欢
  • 1970-01-01
  • 2015-09-22
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多