Google apps script &引用;TypeError:无法读取属性';子串';“未定义”的定义;在两个不同的函数中使用两个子字符串时
我有图像,这些图像作为文件路径存储在电子表格中。通过Google Apps脚本提取图像,方法是创建文件路径名的子字符串,并使用它在预先指定的文件夹中查找图像。我需要为每个数据集提取3个签名,但我只能提取一个Google apps script &引用;TypeError:无法读取属性';子串';“未定义”的定义;在两个不同的函数中使用两个子字符串时,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我有图像,这些图像作为文件路径存储在电子表格中。通过Google Apps脚本提取图像,方法是创建文件路径名的子字符串,并使用它在预先指定的文件夹中查找图像。我需要为每个数据集提取3个签名,但我只能提取一个 function motorElectInstallSignature(row, body){ var signature = row[22]; var sign = signature.substring(signature.indexOf("/") + 1); var sig
function motorElectInstallSignature(row, body){
var signature = row[22];
var sign = signature.substring(signature.indexOf("/") + 1);
var sigFolder = DriveApp.getFolderById("16C0DR-R5rJ4f5_2T1f-ZZIxoXQPKvh5C");
var files = sigFolder.getFilesByName(sign);
var n = 0;
var file;
while(files.hasNext()){
file = files.next();
n++;
} if(n>1){
SpreadsheetApp.getUi().alers('there is more than one file with this name' + sign);
}
var sigElectInstaller = "%SIGNELECTINSTALL%";
var targetRange = body.findText(sigElectInstaller); // Finding the range we need to focus on
var paragraph = targetRange.getElement().getParent().asParagraph(); // Getting the Paragraph of the target
paragraph.insertInlineImage(1, file.getBlob());// As there are only one element in this case you want to insert at index 1 so it will appear after the text // Notice the .getBlob()
paragraph.replaceText(sigElectInstaller, ""); // Remove the placeholder
}
此功能按预期工作,没有错误。
下一个函数是发生错误的地方
function motorCommEngineerSig(row, body){
var signature1 = row[69];
var signCommEng = signature1.substring(signature1.indexOf("/") + 1);
var sigFolder = DriveApp.getFolderById("16C0DR-R5rJ4f5_2T1f-ZZIxoXQPKvh5C");
var files = sigFolder.getFilesByName(signCommEng);
var n = 0;
var file;
while(files.hasNext()){
file = files.next();
n++;
} if(n>1){
SpreadsheetApp.getUi().alers('there is more than one file with this name' + signCommEng);
}
var sigCommEng = "%SFCE%";
var targetRange = body.findText(sigCommEng); // Finding the range we need to focus on
var paragraph = targetRange.getElement().getParent().asParagraph(); // Getting the Paragraph of the target
paragraph.insertInlineImage(1, file.getBlob());// As there are only one element in this case you want to insert at index 1 so it will appear after the text // Notice the .getBlob()
paragraph.replaceText(sigCommEng, ""); // Remove the placeholder
}
此行出现错误:var signcommog=signature1.substring(signature1.indexOf(“/”)+1)代码>
正如您所看到的,除了一些变量名和行号之外,这两个函数之间没有区别。这些函数在我之前编写的脚本中多次按预期工作。这些函数在这里的作用与我之前的脚本中相同,它们的编写方式完全相同
可以找到speradsheet
脚本可以在MotorCommonEngineerSig()中找到我认为您的问题在于如何定义signature1
:
行[69]
指数据中的第70列(0索引)。但是,在先前定义的行中,您只提供了47列数据:
var data = sheet.getRange(2, 2, 10, 47).getValues();
.
.
.
var row = data[rowNumber];
在motorElectInstallSignature()
中,您在第22列中定义了签名,该签名位于数据范围内,以及它为什么对该函数起作用。来自:
当传递给函数的操作数或参数与该运算符或函数预期的类型不兼容时,或当试图修改无法更改的值时,或当试图以不适当的方式使用值时,可能会引发TypeError
这基本上是因为您从row获得的元素,在您的例子中,row[69]
没有substring
方法。这可能是因为:
该元素存在于行中,但不是字符串类型。元素可以是数字、对象等
元素不在列表中(列表中不包含那么多项)。在这种情况下,行[69]
表达式返回未定义的
,该表达式不具有该方法,因此在调用时返回错误
根据您的特定错误消息,第二个选项似乎是正在发生的
为了解决您的问题,我建议您使用内置的调试工具,如或
此外,我还建议您查看以下链接:。听起来输入数据有问题。您能提供一些遇到错误的示例数据吗?我已经添加了包含数据的电子表格。谢谢你能提供的任何帮助@AdamStevensonHello Ciaran,您能描述一下您得到的确切错误吗?此外,关于您的输入数据,您能描述一下您的文件路径存储在哪里吗?我还没找到他们。非常感谢。@carlesgg97这些图像存储在一个名为“签名”的文件夹中。电子表格中与图像相关的数据是指向签名文件夹中特定图像的文件路径。我遇到的错误出现在motorCommonEngineerSig(行,正文)
中的子字符串上。我需要创建文件路径的子字符串,从/
之后的第一个字符开始,以获取文件名,该文件名允许我查找图像并将其附加到模板中。在函数motorElectInstallSignature(行、正文)
中没有问题。typeError仅在MotorCommorEngineerSig(row,body)
@CiaranCrowley您好,您是否能够用以下任何答案解决问题?我有一个类似的问题,想知道你的问题的结果。。。谢谢
var data = sheet.getRange(2, 2, 10, 47).getValues();
.
.
.
var row = data[rowNumber];