Javascript 将数组从GAS脚本传递到html文件、全局J.S.变量
我有下面的GoogleApps脚本,它根据GoogleSheet的值填充一个数组,并创建一个web应用程序。我的代码的顶部已经过测试,运行良好Javascript 将数组从GAS脚本传递到html文件、全局J.S.变量,javascript,google-apps-script,google-apps,Javascript,Google Apps Script,Google Apps,我有下面的GoogleApps脚本,它根据GoogleSheet的值填充一个数组,并创建一个web应用程序。我的代码的顶部已经过测试,运行良好 function doGet() { return HtmlService.createHtmlOutputFromFile('FormFile') .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function ChannelList() { var InfoSheetIter
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>
点击我!
//声明全局变量
window.onload=populatearray;
函数getChannelList(值){
window.ChannelList=值;
}
函数populateArrays(){
google.script.run.withSuccessHandler(getChannelList.ChannelList();
}
功能测试仪(){
document.getElementById(“demo”).innerHTML=window.ChannelList[0][0];
}
编辑:我尝试过的事情:
-添加多个脚本标记。-设置打印通道列表值的超时。 -全局变量 -本地存储窗口变量
我错过了一些东西。当我在getChannelList()方法中打印ChannelList的值时,它工作正常,但当我尝试访问该范围之外的值时。。。没有这样的运气。添加一个
窗口。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>
window.getChannelList=函数(rtrnValue){//在服务器函数完成后运行
//ToDo-处理返回值
}
window.onload=函数(){
google.script.run
.withSuccessHandler(getChannelList)//定义要在完成时运行的函数
.ChannelList();//调用服务器函数
console.log(window.ChannelList[0][0]);
};
我知道至少有几个选项。您可以将数据放入浏览器窗口对象中,该对象在会话中保持不变<代码>window.ChannelList=值代码>可以将内容存储在隐藏的HTML标记中。您可以将内容保存到浏览器的本地存储中@SandyGood尝试了除隐藏html标记方法以外的所有方法,但迄今为止没有成功。是否有标准方法将值从应用程序脚本函数传递到web应用程序的javascript全局变量。是否检查了返回的值,以及getChannelList(value)
函数是否接收到该值console.log('value:'+value)
当它进入浏览器时返回的值是什么?@SandyGood是的,我检查了它,它正在打印正确的值,这就是为什么我如此困惑的原因。该值是一个字符串数组。虽然typeof(value)给出的对象不是数组,而是对象。如果使用window.ChannelList=value代码>,您可以立即尝试将该值作为测试输出<代码>控制台.log('值:'+窗口.ChannelList)代码>如果这样做有效,那么问题在于您没有显示的其他一些代码。您没有显示任何代码来获取存储值。不幸的是,您无法在调用函数内部或调用后立即获取返回值。您需要获取返回的值,然后从success handler函数继续处理。确定。我应该想到这一点。