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

借助Google表格与App Script批量替换多份Google Docs文本

批量替换多份Google Docs文档文本的求助

我参考了Stack Overflow上的《Bulk Find and Replace》以及自己此前发布的《Replace Text in Multiple Document》帖子,尝试通过Google表格中的「Document ID」列表和「Text To Replace」列表,对多份Google Docs文档执行文本替换操作。

相关列表

  • 待替换的词汇列表:[Word to Replace List]
  • 需要执行文本替换的文档ID列表:[Documents ID List]

编写的代码

function bulkreplaceText() {

 // DATA LIST DOC ID DI SPREADSHEET

 const spreadsheet     = SpreadsheetApp.getActiveSpreadsheet();
 const sheetIDList     = spreadsheet.getSheetByName('DOC TO REPLACE');
 const colId           = 3;
 const firstRowID      = 2;
 const lastRowId       = sheetIDList.getLastRow();
 const n               = lastRowId - firstRowID;

 // TEXT TO REPLACE DATA  AT  SPREADSHEET

 const sheetTextToRplc = spreadsheet.getSheetByName('LIST WORD TO REPLACE');
 const colExstText     = 2 ;
 const firstRowRplcTxt = 2 ;
 const lastRowRplcTxt  = sheetTextToRplc.getLastRow();
 const nRow            = lastRowRplcTxt - firstRowRplcTxt + 1
 const rangeRplc       = 
 sheetTextToRplc.getRange(firstRowRplcTxt,colExstText,nRow,2).getA1Notation() ;


 let   startRow    = firstRowID     // First Row DOC ID 
 var   values      = sheetIDList.getRange(startRow, colId, n).getDisplayValues();

       values.forEach(([docId]) => {
           if (!docId) return;

 var doc           = DocumentApp.openById(docId);
 var rgtxt         = doc.getBody();
 var rgrep         = sheetTextToRplc.getRange(rangeRplc);
 var repA          = rgrep.getValues().filter(r => r.every(c => c.toString()));

 repA.forEach(e => rgtxt.replaceText(...e));

         
    });
 }

恳请各位提供技术支持与帮助。

内容的提问来源于stack exchange,提问作者PSAB Plalangan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 16:33:33