问题描述
我有一个Google表单,它在提交时调用一个表单提交函数,其中包含javascript .substring(start,finish)方法。奇怪的是,在我的测试代码中它工作正常,但是当它实际上必须用on submit代码实现时,我得到一个错误消息:
TypeError:无法调用未定义的方法substring。 (第26行,代码文件)
这是我的测试代码,它可以正常工作(调用Browser.msgBox)
function test()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; //假设包含标题列的工作表是第一张工作表
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var values = SpreadsheetApp.getActiveSheet()。getRange(1,1,1,lastColumn).getValues();
//带有表单提交数据的变量将会放在这里,但不会用于这个测试
var dataDump =\\\
\\\
var headerValue = values [0] [5];
var headerValue5 = headerValue.substring(0,5);
Browser.msgBox(headerValue5);
}
这段代码会产生一个错误...
函数onFormSubmit(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; //假设包含标题列的工作表是第一张工作表
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var values = SpreadsheetApp.getActiveSheet()。getRange(1,1,1,lastColumn).getValues();
//定义变量
var timeStamp = e.values [0];
var name = e.values [1];
var email = e.values [2];
var emailSubject =QMG周期性wRVU回应;
emailSubject = emailSubject + timeStamp;
var dataDump =\\\
\\\
;
for(var i = 3; i {
var headerValue = values [0] [i];
var responseValue = e.values [i];
var responseValue5 = responseValue.substring(0,5); //错误在这里!!!!!!
i ++; //增加i得到detailsValue
var detailsValue = e.values [i];
dataDump = dataDump + headerValue;
dataDump = dataDump +:\\\
\\\
;
dataDump = dataDump + responseValue;
dataDump = dataDump +\\\
;
dataDump = dataDump + detailsValue;
dataDump = dataDump +\\\
\\\
********************* \\\
\\\
;
}
MailApp.sendEmail(email,emailSubject,dataDump);
}
删除带注释的行// error here !!!!!!工作正常,所以我知道它正在读取阵列数据。计划是使用.substring()来确定一些格式,所以想得到这个工作。
预先感谢...
p $ p> var array = [aaa,bbb,ccc];for(var i = 0; i< = array.length; i ++){
console.log(array [i]);
}
运行上面的代码时,我会看到一个console.log由于 i ,未定义,
。 / p>
在 undefined throws中运行字符串方法,如 .substring
$ b
将< = 更改为< ,你应该很好去。
I have a google form that when submitted calls an "on form submit" function that includes javascript .substring(start,finish) method. The strange thing is that in my testcode it works fine but when it actually has to implement with the "on submit" code I get an error that says:TypeError: Cannot call method "substring" of undefined. (line 26, file "Code")
Here is my test code which works fine (calls a Browser.msgBox)
function test() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; //assumes that sheet containing header columns is first sheet var lastRow = sheet.getLastRow(); var lastColumn = sheet.getLastColumn(); var values = SpreadsheetApp.getActiveSheet().getRange(1, 1, 1, lastColumn).getValues(); //variables with form submit data would go here but not for this testing var dataDump = "\n\n" var headerValue = values[0][5]; var headerValue5 = headerValue.substring(0,5); Browser.msgBox(headerValue5); }
This code generates an error...
function onFormSubmit(e) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; //assumes that sheet containing header columns is first sheet var lastRow = sheet.getLastRow(); var lastColumn = sheet.getLastColumn(); var values = SpreadsheetApp.getActiveSheet().getRange(1, 1, 1, lastColumn).getValues(); //define variables var timeStamp = e.values[0]; var name = e.values[1]; var email = e.values[2]; var emailSubject = "QMG periodic wRVU responses for "; emailSubject = emailSubject + timeStamp; var dataDump = "\n\n"; for (var i = 3; i <= e.values.length; i++) { var headerValue = values[0][i]; var responseValue = e.values[i]; var responseValue5 = responseValue.substring(0,5); //error here !!!!!! i++; //increment i to get detailsValue var detailsValue = e.values[i]; dataDump = dataDump + headerValue; dataDump = dataDump + ":\n\n"; dataDump = dataDump + responseValue; dataDump = dataDump + "\n"; dataDump = dataDump + detailsValue; dataDump = dataDump + "\n\n*********************\n\n"; } MailApp.sendEmail(email, emailSubject, dataDump); }
Removing line with comment "//error here !!!!!!" works fine so I know that it is reading the array data fine. Plan was to use the .substring() to determine some formatting so would like to get this working.
Thanks in advance...
no pro here per se, but I think I have a hunch on what the issue is.
var array = ["aaa", "bbb", "ccc"]; for (var i = 0; i <= array.length; i++) { console.log(array[i]); }
See when running the above code, I'll get a console.log at the end with undefined,
due to the i <= array.length.
Running a string method like .substring on undefined throws the error.
Change the <= to a < and you should be good to go.
这篇关于谷歌应用脚本TypeError:无法调用方法“substring”的未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!