Google Apps Script替换Google Doc内容异常问题求助
问题与解决思路
需求说明
- 从表格读取文件名,根据Yes/No选项决定处理逻辑:
- 选Yes:从指定文件夹找到对应快捷方式,指向每周更新的原文件,替换目标文件夹中同名现有文件的内容(不能删除替换,否则File ID变更会导致同步中断)
- 选No:跳过该文件
问题现象
执行代码后,目标文件夹中的现有文件内容被清空,仅显示“Blob”字样,原文件中的图片、表格、文本等内容无法正常复制。
问题代码
function copyPolicies() { var spreadsheetId = 'xxxxxxxxxxxxxx'; // Replace with your spreadsheet ID var sheetName = 'xxxxxxx'; // Replace with your sheet name var folderId = 'xxxxxxxxxx'; // Replace with your folder ID var destinationFolderId = 'xxxxxxxxxxxxxx'; // Replace with your destination folder ID var spreadsheet = SpreadsheetApp.openById(spreadsheetId); var sheet = spreadsheet.getSheetByName(sheetName); var data = sheet.getDataRange().getValues(); var folder = DriveApp.getFolderById(folderId); var destinationFolder = DriveApp.getFolderById(destinationFolderId); var fileArray = []; // Array to store filenames and file objects for (var i = 1; i < data.length; i++) { var fileNameYes = data[i][1]; // Assuming the file names for "Yes" option are in the second column var fileNameNo = data[i][0]; // Assuming the file names for "No" option are in the first column var option = data[i][4]; // Assuming the drop-down options are in the fifth column (index 4) if (option === 'Yes') { // Modify 'Yes' to match the option value in your drop-down var files = folder.getFilesByName(fileNameYes); while (files.hasNext()) { var file = files.next(); if (file.getMimeType() === MimeType.SHORTCUT) { var targetFileId = file.getTargetId(); var targetFile = DriveApp.getFileById(targetFileId); // Check if a file with the same name already exists in the destination folder var existingFiles = destinationFolder.getFilesByName(fileNameYes); if (existingFiles.hasNext()) { var existingFile = existingFiles.next(); existingFile.setContent(targetFile.getBlob()); // Overwrite the content of the existing file } else { // Create a new copy of the file in the destination folder var newFile = targetFile.makeCopy(destinationFolder); // Set the copied file's name to the original file name newFile.setName(fileNameYes); fileArray.push({ name: fileNameYes, // Store the file name file: newFile // Store the copied file }); } } } if (!fileArray.length) { Logger.log('File "' + fileNameYes + '" is missing in the given folder or is not a shortcut.'); } } else if (option === 'No') { // Modify 'No' to match the option value in your drop-down Logger.log('Skipping file "' + fileNameNo + '". Option is set to "No".'); } } }
解决思路
1. 核心问题定位
代码中existingFile.setContent(targetFile.getBlob())是错误操作:
setContent()仅接受字符串类型参数,传入Blob对象时,会将Blob的字符串表示(即"Blob")写入文件,直接覆盖原有内容导致无效文本。- 正确方法是使用
setBlob(),该方法专门用于接收Blob对象覆盖文件内容。
2. 修正关键代码行
将:
existingFile.setContent(targetFile.getBlob());
替换为:
existingFile.setBlob(targetFile.getBlob());
3. Google原生文件的特殊处理
如果目标文件是Google Docs/Sheets/Slides这类原生文件,直接用Blob无法正确复制编辑内容(DriveApp的Blob是导出格式,而非原生编辑结构),需用对应API处理:
- Google Docs:打开原文档和目标文档,清空目标内容后复制原文档的所有元素
- Google Sheets:复制原表格的所有工作表到目标表格,再删除目标表格原有工作表
- 示例(Google Docs):
if (targetFile.getMimeType() === MimeType.GOOGLE_DOCS) { var sourceDoc = DocumentApp.openById(targetFileId); var targetDoc = DocumentApp.openById(existingFile.getId()); // 清空目标文档 targetDoc.getBody().clear(); // 复制原文档内容 var elements = sourceDoc.getBody().getChildren(); for (var j = 0; j < elements.length; j++) { targetDoc.getBody().appendChild(elements[j].copy()); } targetDoc.saveAndClose(); } else { // 非原生文件用setBlob existingFile.setBlob(targetFile.getBlob()); }
4. 额外优化建议
- 增加MimeType判断,确保仅处理支持内容替换的文件类型
- 处理目标文件夹中存在多个同名文件的情况(当前代码仅取第一个同名文件,可按需调整逻辑)
内容的提问来源于stack exchange,提问作者user1547226
相关产品推荐
相关产品推荐

