【发布时间】: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