将数组从 GAS 脚本传递到 html 文件,全局 J.S。变量
Passing arrays from GAS script to html file, Global J.S. variables
我有以下 google 应用程序脚本,它根据 google-sheet 的值填充数组并创建网络应用程序。我的代码的顶部已经过测试并且工作正常。
function doGet() {
return HtmlService.createHtmlOutputFromFile('FormFile')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function ChannelList() {
var InfoSheetIterator = DriveApp.getFilesByName("InfoSheets");
var InfoSheetFile = InfoSheetIterator.next();
var InfoSheet = SpreadsheetApp.open(InfoSheetFile);
var NumChannels = getLastRow('A');
var ChannelTwoDimArray = InfoSheet.getRange('A2:A'+String(NumChannels)).getValues();
return ChannelTwoDimArray;
}
然后我正在尝试在 Javascript 中将数组声明为全局变量,然后使用此代码将该数组用于其他目的。我得到一个空数组,我不确定为什么。任何提示表示赞赏。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button type="button" onclick="tester()">Click Me!</button>
<p id = "tester"></p>
<script>
//declaring global variables
window.onload = populateArrays;
function getChannelList(value) {
window.ChannelList = value;
}
function populateArrays() {
google.script.run.withSuccessHandler(getChannelList).ChannelList();
}
function tester() {
document.getElementById("demo").innerHTML = window.ChannelList[0][0];
}
</script>
</body>
</html>
编辑:我尝试过的事情:
- 添加多个脚本标签。
- 为 ChannelList 值的打印设置超时。
- 全局变量
- 本地存储 window 变量
我在这里遗漏了一些东西。当我在 getChannelList() 方法中打印 ChannelList 的值时它工作正常,但是当我尝试在该范围之外访问它的值时......没有这样的运气。
添加一个 window.onload
事件和一个成功处理程序
<script>
window.getChannelList = function(rtrnValue) {//Runs AFTER the server function completes
//ToDo - Process the return value
}
window.onload = function() {
google.script.run
.withSuccessHandler(getChannelList)//define the function to run on completion
.ChannelList();//Call the server function
console.log(window.ChannelList[0][0]);
};
</script>
我有以下 google 应用程序脚本,它根据 google-sheet 的值填充数组并创建网络应用程序。我的代码的顶部已经过测试并且工作正常。
function doGet() {
return HtmlService.createHtmlOutputFromFile('FormFile')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function ChannelList() {
var InfoSheetIterator = DriveApp.getFilesByName("InfoSheets");
var InfoSheetFile = InfoSheetIterator.next();
var InfoSheet = SpreadsheetApp.open(InfoSheetFile);
var NumChannels = getLastRow('A');
var ChannelTwoDimArray = InfoSheet.getRange('A2:A'+String(NumChannels)).getValues();
return ChannelTwoDimArray;
}
然后我正在尝试在 Javascript 中将数组声明为全局变量,然后使用此代码将该数组用于其他目的。我得到一个空数组,我不确定为什么。任何提示表示赞赏。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button type="button" onclick="tester()">Click Me!</button>
<p id = "tester"></p>
<script>
//declaring global variables
window.onload = populateArrays;
function getChannelList(value) {
window.ChannelList = value;
}
function populateArrays() {
google.script.run.withSuccessHandler(getChannelList).ChannelList();
}
function tester() {
document.getElementById("demo").innerHTML = window.ChannelList[0][0];
}
</script>
</body>
</html>
编辑:我尝试过的事情:
- 添加多个脚本标签。
- 为 ChannelList 值的打印设置超时。
- 全局变量
- 本地存储 window 变量
我在这里遗漏了一些东西。当我在 getChannelList() 方法中打印 ChannelList 的值时它工作正常,但是当我尝试在该范围之外访问它的值时......没有这样的运气。
添加一个 window.onload
事件和一个成功处理程序
<script>
window.getChannelList = function(rtrnValue) {//Runs AFTER the server function completes
//ToDo - Process the return value
}
window.onload = function() {
google.script.run
.withSuccessHandler(getChannelList)//define the function to run on completion
.ChannelList();//Call the server function
console.log(window.ChannelList[0][0]);
};
</script>