当特定单元格更改值谷歌表格时播放声音

问题描述 投票:0回答:2

我正在尝试运行这篇文章的代码,:

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

在 Rob Blakemore 的推荐下

有人可以给我一张使用此代码的工作表吗?

我尝试在工作表中测试此代码,但它对我不起作用。

它只向我发送消息:正在检查呼叫...

我向您发送我的表格链接:

https://docs.google.com/spreadsheets/d/1rdm0zmZMherO8IspDb3Ce9n0j9tLYFYtM9V8fIX8uV4/edit#gid=0

这是应用程序脚本的代码:

Code:
// 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]; });

页码:

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

    <audio id="call">
      <!-- <source src="http://docs.google.com/uc?export=open&id=1CfWaMX6wfJSSdp-u_rU0bkRpwNmXWpVu/view?usp=sharing" type="audio/mp3"> -->

<source src="http://docs.google.com/uc?export=open&id=1CfWaMX6wfJSSdp-u_rU0bkRpwNmXWpVu/view?usp=sharing" 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();
    }

    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>
  </body>
</html>
javascript html google-apps-script google-sheets html5-audio
2个回答
0
投票

在您的脚本中,按如下方式更改 mp3 文件的端点怎么样?

来自:

<source src="http://docs.google.com/uc?export=open&id=1CfWaMX6wfJSSdp-u_rU0bkRpwNmXWpVu/view?usp=sharing" type="audio/mp3">

致:

<source src="https://drive.google.com/uc?id=1CfWaMX6wfJSSdp-u_rU0bkRpwNmXWpVu&export=download" type="audio/mp3">
  • 在这种情况下,使用
    webContentLink

注:

  • 当我通过反映此修改来测试您的脚本时,我确认您的 mp3 文件已播放。

-2
投票

您能帮我在我的一张谷歌表格中实现声音吗?电子邮件 [email protected]

非常感谢您的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.