Google Apps Script提取签名时触发TypeError: Cannot read property 'getBlob' of undefined
Hey there, let's dig into why your commEngineerSignature function is throwing that frustrating TypeError while electInstallSignature works perfectly. The core issue here is that the file variable in your comm engineer function ends up undefined when you try to call file.getBlob(). Let's break down the possible causes and fix them step by step.
Why This Happens
Your two functions look identical on paper, but there are a few key spots where things can go wrong specifically for the comm engineer signature:
- Wrong column index:
row[35]might not map to the correct cell in your spreadsheet. - Invalid/missing signature value: The cell could be empty, or the string doesn't contain a
/(breaking your substring logic). - No matching file in Drive: The extracted
signstring doesn't match any filename in your signature folder. - Row count mismatch: Your
datarange uses 10 rows, but yourtagsrange uses 11—this can lead to pulling an invalid row entirely.
Step-by-Step Fixes
1. Add Robust Error Handling to commEngineerSignature
Update the function to catch issues before they trigger the TypeError. This will also give you clear alerts about what's going wrong:
function commEngineerSignature(row, body){ var signature = row[35]; // Check if the signature cell is empty if (!signature) { SpreadsheetApp.getUi().alert('Comm Engineer signature value is missing from the row.'); return; } // Check if the signature string has a "/" to split on var slashIndex = signature.indexOf("/"); if (slashIndex === -1) { SpreadsheetApp.getUi().alert('Comm Engineer signature format is invalid: no "/" found in the value.'); return; } var sign = signature.substring(slashIndex + 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++; } // Check if no matching file was found if(n === 0){ SpreadsheetApp.getUi().alert('No file found for Comm Engineer signature: ' + sign); return; } if(n>1){ SpreadsheetApp.getUi().alert('There is more than one file with this name: ' + sign); } var sigCommEngineer = "%SFCE%"; var targetRange = body.findText(sigCommEngineer); // Check if the placeholder exists in the template if (!targetRange) { SpreadsheetApp.getUi().alert('Placeholder %SFCE% not found in the document template.'); return; } var paragraph = targetRange.getElement().getParent().asParagraph(); paragraph.insertInlineImage(1, file.getBlob()); paragraph.replaceText(sigCommEngineer, ""); }
2. Fix Row Count Mismatch in Data Ranges
In chooseRowMethodI, your data range uses 10 rows, but your tags range uses 11. This means when i reaches 10 in the loop, rowNumber = i will try to access data[10]—which doesn't exist (since data only has indexes 0-9). Fix this by matching the row counts:
// Change from 10 to 11 to align with the tags range var data = sheet.getRange(2, 2, 11, 41).getValues();
3. Verify Column Mapping
Make sure row[35] points to the right cell. Since data starts at column 2 (B), row[0] = B, row[1] = C, ..., row[35] = AK column. Confirm this cell has a valid signature path (like /JaneDoe_Signature.png).
Final Checks
- Test the updated script—you'll now get specific alerts instead of a vague TypeError.
- Confirm the extracted filename exactly matches a file in your signature folder (Drive filenames are case-sensitive!).
内容的提问来源于stack exchange,提问作者Ciaran Crowley

