【问题标题】:Spreadsheet Alarm Toggle电子表格警报切换
【发布时间】:2021-01-20 17:37:59
【问题描述】:

目前我正在寻找一种方法来为正在查看电子表格的所有用户切换警报:

// creates a custom menu when the spreadsheet is opened
function onOpen() {
  var ui = SpreadsheetApp.getUi()
    .createMenu('SWAT Pager')
    .addItem('Open SWAT Pager', '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("SWAT Pager");

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

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

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

这就是 code.gs

现在我想做的是设置一个可以选择的下拉菜单:

警告/无警告。

当我选择“警告”时,我想要播放警报声,而“无警告”则没有声音。

我试过把这个脚本改成它:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p id="message">Keine Alarmierung </p>

    <audio id="Alarmierung">
      <source src="http://banhammer.bplaced.net/audio/spaceship_alarm.mp3" 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 "getColumnE" 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 (columnE) {
        for (var i = 0; i < columnE.length; i++) {

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

        // store results for next time
        lastTime = columnE;

        console.log(lastTime);

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

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

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

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

但不幸的是,它不起作用。我会感谢社区的任何建议。

【问题讨论】:

  • 当你说它不起作用时,你的意思是你得到一个错误吗?执行屏幕有什么说明吗?您能否详细说明为什么这不能按您期望的方式工作?当你说你想要一个下拉菜单来选择你是否想要声音时,你的意思是你想为每个用户选择他们得到什么样的警报?这段代码是你写的还是有源码?
  • 不,我没有收到错误。我不知道如何更改代码,即当 E 列中的单词“警告”时播放声音,而当单词“无警告”时,声音会播放
  • 可以分享样张吗?或者至少是屏幕截图?
  • prnt.sc/xdp0g6 您可以在 E 列中看到我可以选择的 2 个点 因此,当设置第一个“Alarmierung”时,我希望所有正在查看电子表格的用户都能播放声音.当“Keine Alarmierung”设置在那里时,声音需要停止。

标签: html google-apps-script google-sheets


【解决方案1】:

建议的解决方案

您的代码大体上是朝着正确的方向,但它似乎在做一些必然复杂的事情,例如:

return sheet.getRange(1, 8, sheet.getLastRow(), 1).getValues().map(function (row) { return row[0]; });

从 E2 获取一个值,可以这样完成:

return sheet.getRange("E2").getValue()

虽然您可能出于某种原因想像这样聚合这些值,但在我看来,您似乎太忙于太快地工作,并且对去哪里感到困惑。

无论如何,我采用了您的代码并对其进行了简化以使警报起作用,我相信您的问题真正针对的是:

代码.gs

// This function creates a pop up called "Alarm"
function alarm(){
  var html = HtmlService.createHtmlOutputFromFile('Page')
    .setWidth(400)
    .setHeight(300);
  SpreadsheetApp.getUi()
      .showModalDialog(html, "Alarm");
}

// This is the function called by the client-side js in "Page.html"
// It returns a true or false value depending on the contents of E2
function checkAlarm() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Operationen");
  return sheet.getRange("E2").getValue() == "ALARM"
  
}

页面.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p id="message">Keine Alarmierung </p>
    <script>
      // This is the call to the checkAlarm function in Code.gs.
      // It returns a true or false value which is passes into the function
      // playAlarm
      google.script.run.withSuccessHandler(playAlarm).checkAlarm();

      
      function playAlarm(bool){
        // If the value returned from "checkAlarm" is true
        // It created the HTML audio element and makes it play.
        if (bool == true){
          let audio = document.createElement("audio");
          audio.setAttribute("controls", "controls");
          audio.setAttribute("autoplay", "autoplay"); // So it plays automatically
          audio.setAttribute("style", "display:none"); // So the player is hidden

          let source = document.createElement("source");
          source.setAttribute("src","https://onlineclock.net/audio/options/police-car.mp3");
          source.setAttribute("type","audio/mpeg");

          audio.appendChild(source);
          document.appendChild(audio);
        }
      }

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

开始工作的步骤。

  1. 粘贴代码。
  2. 确保它没有被调用 onOpen - 要让 onOpen 函数能够访问 UI,它需要安装而不是简单的触发器。因此,转到您项目中的触发器并使用alarm 函数安装 onOpen 一个。
  3. 确保OperationenE2 的值为ALARM
  4. 刷新页面,闹钟就会响起!

注意事项

很遗憾,您的声音源 http://banhammer.bplaced.net/audio/spaceship_alarm.mp3 在 Apps 脚本中不起作用。我相信这是因为它是http 原点而不是https 原点。这就是为什么我用另一个声音 https://onlineclock.net/audio/options/police-car.mp3 替换它以使其正常工作。

脚本仅在Operationen 中的单元格E2 中检查与ALARM 的匹配。 任何其他值都不会导致报警。

参考文献

【讨论】:

  • 我已经用触发器完成了你所说的一切,脚本也可以,但是声音没有播放。只有弹出窗口没有别的
  • 只是检查一下,单元格 E2 的字面意思是“警报”,对吗?您还用我的代码替换了所有代码?包括声音链接?它对我有用。
  • 是的,我替换了嚎叫代码,是的,细胞出现了警报。弹出窗口有效,但声音无效。不知道为什么
  • 也是我创建的触发器:prnt.sc/xgj7uk
  • 在函数checkAlarm 中尝试将返回行替换为return true - 现在它应该一直播放。如果这不起作用,您在 Chrome 中吗?一旦弹出窗口出现,您可以检查开发工具控制台吗?您的执行页面是否显示任何错误?
猜你喜欢
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多