我有這個腳本,它使用 Google 電子表格中的資訊替換 Google 檔案中的文本。
輸入
輸入是 Google Sheet 中帶有軟換行符的單元格。

我想將那些軟換行符轉換為硬換行符。
到目前為止的解決方案
我可以通過搜索 /\n/g 找到我在 Google 電子表格上寫的軟換行符。但是,當我嘗試用 \r 替換 \n 時,我仍然會遇到軟換行符。
demo_content = ss.getRangeByName("SectionContentsDemo").getValue().toString().replace(/\n/g,'\r\n'),
template_doc_body.replaceText("{{Section Contents - Demo}}", demo_content);
我能夠驗證它是軟換行符而不是硬換行符,因為 Google Doc 段落中的所有行都縮進,因為軟換行符會起作用。

任何人都知道如何將軟斷線替換為硬斷線?
謝謝
資料來源:
https://docs.google.com/spreadsheets/d/1VAU0-COifUd2j1PA5_td3tuDnBSqcnOo_4-cMqiAFUI
https://docs.google.com/document/d/1dQaDipbs3BkMYcHx_1s6qFjyltmLtbTrzzlmVX3Cl1o/
uj5u.com熱心網友回復:
從您問題中的以下腳本以及示例輸入和輸出情況中,
demo_content = ss.getRangeByName("SectionContentsDemo").getValue().toString().replace(/\n/g,'\r\n'),
template_doc_body.replaceText("{{Section Contents - Demo}}", demo_content);
我認為當包含換行符的值{{Section Contents - Demo}}在您的 Google 檔案中被替換時,縮進可能會在第 2 行之后更改。那么,在這種情況下,如何使用 Docs API?使用Docs API時,示例腳本如下。
示例腳本:
在您使用此腳本之前,請在高級 Google 服務中啟用 Docs API。
function myFunction() {
var template_id = '###'; // Please set your template Google Document ID.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var value = ss.getRangeByName("SectionContentsDemo").getValue();
var documentId = DriveApp.getFileById(template_id).makeCopy().getId();
Docs.Documents.batchUpdate({
requests: [
{
replaceAllText: {
replaceText: value,
containsText: { matchCase: true, text: "{{Section Contents - Demo}}" }
}
}]
}, documentId);
}
- 運行此腳本時,會從
SectionContentsDemo電子表格的命名范圍中檢索該值,并將檢索到的值替換{{Section Contents - Demo}}為檔案中的 。在這種情況下,縮進似乎是到{{Section Contents - Demo}}.
參考:
- 方法:documents.batchUpdate
- 替換所有文本請求
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365617.html
標籤:javascript 谷歌应用程序脚本 谷歌表格 谷歌文档 google-docs-api
