本文介绍了带有Google表格的Apps Script的倒数计时器开始/停止/重置按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Google表格上的带有启动/停止/重置命令按钮的倒数计时器存在一些问题.如何通过使用Google Apps脚本为其编写脚本,以及如何通过使用开始/停止/重置"按钮将其放置在Google工作表中.

I have some issues about countdown timer with start/stop/reset command button on Google Sheets . how to write a script for it by using google apps script, and put in google sheets, command by using start/stop/reset button.

推荐答案

您可以将操作附加到图像.这样,您就可以将开始,停止和重置按钮作为图像使用.单击图像可以链接到应用程序中的函数,该函数可以更新单个单元格中的时间.

You can attach actions to images in a google sheet. That way you can have a start, stop and reset buttons as images. When clicked the images can link to functions in the appscript which updates the time in single cell.

下面是代码,这是一个示例工作表.有关如何触发按钮的信息,请参阅将操作附加到图像"文档.

Below is the code, and here is a sample sheet . See the "attach action to images" document on how to trigger the buttons if needed.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');

function  countDownSeconds(seconds, minutes){
  while (minutes >=0){
     sheet.getRange(2,1).setValue(minutes);
    while (seconds >= 0 ){
      var m = sheet.getRange(2,1).getValue()
      var s = sheet.getRange(2,2).getValue()
      if (m === 'pausing' || s === 'pausing'|| m === 'reset' || s === 'reset' ) {
       return
      }
      sheet.getRange(2,2).setValue(seconds);
      SpreadsheetApp.flush();
      Utilities.sleep(1000)
      seconds --
    }
    seconds = 59;
    minutes = sheet.getRange(2,1).getValue();
    minutes --
  }
}

function startTimer(){
   var minutes = sheet.getRange(2,1).getValue()
   var seconds = sheet.getRange(2,2).getValue()
   countDownSeconds(seconds, minutes)

}

function pause(){
   var minutes = sheet.getRange(2,1).getValue()
   var seconds = sheet.getRange(2,2).getValue()
   sheet.getRange(2,1).setValue('pausing');
   sheet.getRange(2,2).setValue('pausing');
   SpreadsheetApp.flush();
   Utilities.sleep(1000);
   sheet.getRange(2,1).setValue(minutes)
   sheet.getRange(2,2).setValue(seconds)
}

function reset(){
   sheet.getRange(2,1).setValue('reset');
   sheet.getRange(2,2).setValue('reset');
   SpreadsheetApp.flush();
   Utilities.sleep(1000);
   sheet.getRange(2,1).setValue(0)
   sheet.getRange(2,2).setValue(0)
}

这篇关于带有Google表格的Apps Script的倒数计时器开始/停止/重置按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!