google-apps-script - 在两个不同的函数中使用两个子字符串时,“TypeError:无法读取未定义的属性‘子字符串’”
问题描述
我有在电子表格中存储为文件路径的图像。图像是通过 Google Apps 脚本提取的,方法是创建文件路径名的子字符串并使用它在预先指定的文件夹中查找图像。我要为每个数据集提取 3 个签名,但我只能提取一个。
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 signCommEng = signature1.substring(signature1.indexOf("/") + 1);
如您所见,两个函数之间没有区别,除了一些变量名和行号。这些函数在我之前编写的脚本中多次按预期工作。这些函数在这里的用途与我之前的脚本中的用途相同,并且它们的编写方式完全相同。
电子表格可以在这里找到
脚本可以在这里找到
解决方案
我认为您的问题在于您如何定义signature1
in motorCommEngineerSig()
:
var signature1 = row[69];
row[69]
指数据中的第 70 列(0 索引)。但是,在您之前定义的地方row
,您只提供了 47 列数据:
var data = sheet.getRange(2, 2, 10, 47).getValues();
.
.
.
var row = data[rowNumber];
在motorElectInstallSignature()
中,您在第 22 列中定义了签名,该签名在数据范围内以及它为何适用于该函数。
推荐阅读
- python - 从python 3中的实例变量中获取参数
- windows - 在 Windows 上使用 gcc/clang 内置函数
- r - 如何处理杂乱的数据 - 变量未标记为缺失
- node.js - res.status 和 res.send 请求发生两次
- android - Xamarin Android:如何在回收站视图中更改选定行的颜色
- python - 填充大量/大块时间序列数据
- php - 如何在 Symfony 4 中使用 PortPHP Bundle 恢复我的属性?
- python - 我可以使用变量来确定列表中的字符串吗?
- c# - 系统对象处理异常
- javascript - 如何移动 HERE 地图信息气泡尾部的位置?