借助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
相关产品推荐
相关产品推荐

