繁体   English   中英

Google 表格 + 应用脚本 + HTML 停止元素在 Google 表格侧边栏中的 onclick function 之后清除

[英]Google sheet + App Script + HTML Stop Element from clearing after onclick function in google sheet sidebar

嗨,我在我的谷歌表上使用侧边栏到 select 开始日期和结束日期我有一个按钮,onclick 在我的谷歌表上设置这些日期并且工作正常我遇到的问题是我想要选择的单击按钮后保留且不被清除的日期 这是我的 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>

谢谢

修改点:

  • 起初,我以为您想在单击按钮时清除输入标签。 当我看到您的示例电子表格时,我看到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 侧的 function Clear()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM