将数组从html表单传递给gs

问题描述 投票:-1回答:2

我正在使用Google App Script。

我已经从Google表格中的值创建了下拉框。我的目的是收集数组中的selectedIndex,将它们作为参数发送到后端,并让它们根据参数从同一个Google Sheet中输出一个值。

我能够成功地将下拉框的选定索引数组从html表单传递到后端(code.gs),但是当我尝试使用getRange和selectedindexes获取Google表格中单元格的值时,我总是得到这些错误:

console.log - 在calculateCL上未被捕获(代码:22)

Logger.log - 能够记录单元格的值,但当我“返回PTvalue”时,前端会将其记录为未定义。

请帮忙!

page.html中

//gets selected index from dropdown
    var CountryIndex = document.getElementById("menu").selectedIndex;
    var CUTypeIndex = document.getElementById("ListCUTyp").selectedIndex;

//pushes indices into array 
    formArray = [];
    formArray.push(CountryIndex);
    formArray.push(CUTypeIndex);

//array sent to back end within function calculateCL
//on success of calculateCL, run function CLOutput
    google.script.run.withSuccessHandler(CLOutput()).calculateCL(formArray)

//outputs value from GS
    function CLOutput(PTvalue){
                console.log(PTvalue);
                document.getElementById("ListPT").value = PTvalue;
            }

code.公司

//gets value of cell based on selected index array
        function calculateCL(formArray) {
          var PTvalue = sheet.getRange(2+formArray[0], 2+formArray[1]).getValue();
          Logger.log(PTvalue);
          return PTvalue;
        }
javascript html google-apps-script html-form
2个回答
0
投票

你的calculateCL有参数但似乎你在没有参数的情况下使用它。

google.script.run.withSuccessHandler(CLOutput()).`calculateCL()`

看看你的功能:

function calculateCL(formArray)

0
投票

这是不正确的:

 google.script.run
.withSuccessHandler(CLOutput())
.calculateCL(formArray);

这是一个正确的形式:

google.script.run
.withSuccessHandler(CLOutput)
.calculateCL(formArray)

 function CLOutput(PTvalue){
   console.log(PTvalue);
   document.getElementById("ListPT").value = PTvalue;
 }

这也是正确的

 google.script.run
.withSuccessHandler(function(PTValue){
    console.log(PTvalue);
    document.getElementById("ListPT").value = PTvalue;
})
.calculateCL(formArray)
© www.soinside.com 2019 - 2024. All rights reserved.