我正在尝试制作一个Web应用程序来跟踪工作时间。座席开始工作时将需要单击一个按钮,完成后将再次单击该按钮。所以我编写了秒表。但是我找不到捕捉秒表最终读数并将其发送到电子表格的方法。有人可以帮我吗?

<script>

    var sw = {
  /* [INIT] */
  /* sw: stopwatch */
  etime : null, // holds HTML time display
  erst : null, // holds HTML reset button
  ego : null, // holds HTML start/stop button
  timer : null, // timer object
  now : 0, // current timer
  //send : null,

 init : function () {
    // Get HTML elements
    sw.etime = document.getElementById("sw-time");
    sw.erst = document.getElementById("sw-rst");
    sw.ego = document.getElementById("sw-go");

    // Attach listeners
    sw.erst.addEventListener("click", sw.reset);
    sw.erst.disabled = false;
    sw.ego.addEventListener("click", sw.start);
    sw.ego.disabled = false;
  },

  /* [ACTIONS] */
  tick : function () {
  // tick() : update display if stopwatch running

    // Calculate hours, mins, seconds
    sw.now++;
    var remain = sw.now;
    var hours = Math.floor(remain / 3600);
    remain -= hours * 3600;
    var mins = Math.floor(remain / 60);
    remain -= mins * 60;
    var secs = remain;

    // Update the display timer
    if (hours<10) { hours = "0" + hours; }
    if (mins<10) { mins = "0" + mins; }
    if (secs<10) { secs = "0" + secs; }
    sw.etime.innerHTML = hours + ":" + mins + ":" + secs;
  },

  start : function () {
  // start() : start the stopwatch
  // change "start" to "check in"

    sw.timer = setInterval(sw.tick, 1000);
    sw.ego.value = "Check Out";
    sw.ego.removeEventListener("click", sw.start);
    sw.ego.addEventListener("click", sw.stop);
  },

  stop  : function () {
  // stop() : stop the stopwatch
  //change to check out button
  //send the last reading to the spreadsheet??

    clearInterval(sw.timer);
    sw.timer = null;
    sw.ego.value = "Check In";
    sw.ego.removeEventListener("click", sw.stop);
    sw.ego.addEventListener("click", sw.start);

  },

  reset : function () {
  // reset() : reset the stopwatch

    // Stop if running
    if (sw.timer != null) { sw.stop(); }

    // Reset time
    sw.now = -1;
    sw.tick();
  }
};

window.addEventListener("load", sw.init);

    </script>

最佳答案

如果您正在Web应用程序的html页面中运行此程序。然后,您可以使用以下命令将其发送到服务器端功能:

google.script.run.withSuccessHandler(function(msg){
  window.alert(msg);
}).saveTime(sw.now);


saveTime()函数就是这样。

function saveTime(count) {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet1');
  sh.getRange("A1").setValue(count/60);//for minutes
  SpreadsheetApp.flush();
  return "Thanks I got it";
}

关于javascript - 如何使用Apps脚本将秒表的最终读数发送到电子表格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59517807/

10-13 09:07