谷歌工作表 + 应用脚本 + HTML 停止元素在谷歌工作表侧边栏中的 onclick 功能后清除

时间:2021-06-02 20:20:50

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

嗨,我正在使用 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 代码的一部分,我尝试添加“返回假”但没有用

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

谢谢

1 个答案:

答案 0 :(得分:0)

修改点:

  • 起初,我以为您要在单击按钮时清除输入标记。当我看到您的示例电子表格时,我看到了 I need those 2 field above to retain there selected values after clicking the post buttonwhat 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 DateTo 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);
}

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

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