You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script提取签名时触发TypeError: Cannot read property 'getBlob' of undefined

Fixing the "Cannot read property 'getBlob' of undefined" Error in Your Google Apps Script

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:

  1. Wrong column index: row[35] might not map to the correct cell in your spreadsheet.
  2. Invalid/missing signature value: The cell could be empty, or the string doesn't contain a / (breaking your substring logic).
  3. No matching file in Drive: The extracted sign string doesn't match any filename in your signature folder.
  4. Row count mismatch: Your data range uses 10 rows, but your tags range 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:05:50