![](/img/trans.png)
[英]Hard Line Break in Google Apps Script (Google Docs with Google Form Field to create new bullet in bulleted list)
[英]Google Apps Script: Replace soft line break for hard line break
我有這個腳本,它使用來自谷歌電子表格的信息替換谷歌文檔中的文本。
輸入
輸入是 Google 表格中的一個單元格,其中包含軟換行符。
我想將那些軟換行符轉換為硬換行符。
到目前為止的解決方案
我可以通過搜索 /\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/
從您的問題中的以下腳本以及您的示例輸入和 output 情況,
demo_content = ss.getRangeByName("SectionContentsDemo").getValue().toString().replace(/\n/g,'\r\n'),
template_doc_body.replaceText("{{Section Contents - Demo}}", demo_content);
我認為當包含換行符的值在您的 Google 文檔中被替換為{{Section Contents - Demo}}
時,縮進可能會在第 2 行之后更改。 那么,在這種情況下,使用文檔 API 怎么樣? 使用Docs API時,示例腳本如下。
在使用此腳本之前, 請在 Advanced Google services 中啟用 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}}
之后。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.