I have images which are stored as file paths in a spreadsheet. The image is extracted via Google Apps Script by creating a substring of their file path name and using it to find the image in a pre specified folder. I to extract 3 signatures per data set, but I am only able to extract one.
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
}
This function works as intended with no errors. This next function is where the error occurs.
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
}
The error occurs on this line:var signCommEng = signature1.substring(signature1.indexOf("/") + 1);
As you can see, there is no difference between both functions, with the exception of some variable names and a row number. These functions work as intended multiple times in a script that I have previously written. These functions serve the same purpose here as they did in my previous script and they are written the exact same way.
The speradsheet can be found here
The Script can be found here
substringinmotorCommEngineerSig(row, body). I need to make a substring of the file path, starting at the very first character after/to get the file name which allows me to find the image and append it to a template. In the functionmotorElectInstallSignature(row, body)there are no problems. The typeError only persists inmotorCommEngineerSig(row, body).