Google脚本:在特定单元格更改值时播放声音

时间:2016-11-30 06:21:19

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

情况:

Example Spreadsheet

  

表:支持
  列:H具有以下功能" = IF(D:D> 0; IF($ B $ 1> = $ G:G;" Call&#34 ;;" In时间");"")" 根据结果更改值。

问题:

我需要:

  
      
  1. 当H列中的单元格变为" Call"在表格上"支持"。
  2.   
  3. 此功能需要每隔5分钟运行一次。
  4.   
  5. 是否需要将声音上传到云端硬盘,还是可以使用来自网址的声音?
  6.   

我很感激任何人都可以提供帮助...我看到很多代码,但我不太了解。

2 个答案:

答案 0 :(得分:4)

这是一个非常棘手的问题,但可以使用侧边栏来定期轮询H列以进行更改。

<强> Code.gs

// creates a custom menu when the spreadsheet is opened
function onOpen() {
  var ui = SpreadsheetApp.getUi()
    .createMenu('Call App')
    .addItem('Open Call Notifier', 'openCallNotifier')
    .addToUi();

  // you could also open the call notifier sidebar when the spreadsheet opens
  // if you find that more convenient
  // openCallNotifier();
}

// opens the sidebar app
function openCallNotifier() {
  // get the html from the file called "Page.html"
  var html = HtmlService.createHtmlOutputFromFile('Page') 
    .setTitle("Call Notifier");

  // open the sidebar
  SpreadsheetApp.getUi()
    .showSidebar(html);
}

// returns a list of values in column H
function getColumnH() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Support");

  // get the values in column H and turn the rows into a single values
  return sheet.getRange(1, 8, sheet.getLastRow(), 1).getValues().map(function (row) { return row[0]; });
}

<强> page.html中

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p id="message">Checking for calls...</p>

    <audio id="call">
      <source src="||a URL is best here||" type="audio/mp3">
      Your browser does not support the audio element.
    </audio>

    <script>
    var lastTime = []; // store the last result to track changes

    function checkCalls() {

      // This calls the "getColumnH" function on the server
      // Then it waits for the results
      // When it gets the results back from the server,
      // it calls the callback function passed into withSuccessHandler
      google.script.run.withSuccessHandler(function (columnH) {
        for (var i = 0; i < columnH.length; i++) {

          // if there's a difference and it's a call, notify the user
          if (lastTime[i] !== columnH[i] && columnH[i] === "Call") {
            notify();
          }
        }

        // store results for next time
        lastTime = columnH;

        console.log(lastTime);

        // poll again in x miliseconds
        var x = 1000; // 1 second
        window.setTimeout(checkCalls, x);
      }).getColumnH();
    }

    function notify() {
      document.getElementById("call").play();
    }

    window.onload = function () {
      checkCalls();
    }

    </script>
  </body>
</html>

有些消息来源:

答案 1 :(得分:0)

当我实现给出的主要答案时,递归调用checkCalls()最终会导致错误(这基本上是正确的,而且非常有用,所以谢谢!)。

//注意:但是最初的实现可以在一段时间内正常工作-例如90分钟-然后崩溃。通常需要1秒钟的呼叫将花费300秒,而执行将暂停。看起来它通过继续递归调用自身而炸毁了堆栈。当在适当退出函数的情况下将其移至对check()的单次调用时,它便开始工作。

Chrome在运行JavaScript的控制台日志中说: ERR_QUIC_PROTOCOL_ERROR.QUIC_TOO_MANY_RTOS 200

经过大量研究,我找到了一种更好的方法……不需要递归(因此不会造成麻烦)

删除此行: // window.setTimeout(checkCalls,500);

并在脚本末尾使用类似这样的内容:

  // This function returns a Promise that resolves after "ms" Milliseconds

        // The current best practice is to create a Promise...
  function timer(ms) {
   return new Promise(res => setTimeout(res, ms));
  }

  
  async function loopthis () { // We need to wrap the loop into an async function for the await call (to the Promise) to work.  [From web: "An async function is a function declared with the async keyword. Async functions are instances of the AsyncFunction constructor, and the await keyword is permitted within them. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains."]
    for (var i = 0; i >= 0; i++) {
      console.log('Number of times function has been run: ' + i);
      checkCalls();
      await timer(3000);
    }
  }


  window.onload = function () {
    loopthis();
  }

</script>