[英]Google Apps Script: Replace soft line break for hard line break
I have this script that replaces text in a Google Doc using info from a Google Spreadsheet.我有这个脚本,它使用来自谷歌电子表格的信息替换谷歌文档中的文本。
Input输入
The input is a cell inside a Google Sheet that has soft line breaks.输入是 Google 表格中的一个单元格,其中包含软换行符。
I would like to convert those soft line breaks into hard line breaks.我想将那些软换行符转换为硬换行符。
Solution so far到目前为止的解决方案
I'm able to find the soft line break I write on the Google Spreadsheet by searching for /\n/g.我可以通过搜索 /\n/g 找到我在 Google 电子表格上写的软换行符。 However when I try to replace \n with \r I'm still getting soft line breaks.但是,当我尝试用 \r 替换 \n 时,我仍然会遇到软换行符。
demo_content = ss.getRangeByName("SectionContentsDemo").getValue().toString().replace(/\n/g,'\r\n'),
template_doc_body.replaceText("{{Section Contents - Demo}}", demo_content);
I'm able to validate that it's a soft line break instead of a hard one because all the lines in the Google Doc paragraph are indented, as a soft line break would behave.我能够验证它是软换行符而不是硬换行符,因为 Google Doc 段落中的所有行都是缩进的,因为软换行符会起作用。
Anyone has an idea on how to replace the soft line breaks into hard ones?有人知道如何将软线换成硬线吗?
Thanks谢谢
Sources:资料来源:
https://docs.google.com/spreadsheets/d/1VAU0-COifUd2j1PA5_td3tuDnBSqcnOo_4-cMqiAFUI https://docs.google.com/spreadsheets/d/1VAU0-COifUd2j1PA5_td3tuDnBSqcnOo_4-cMqiAFUI
https://docs.google.com/document/d/1dQaDipbs3BkMYcHx_1s6qFjyltmLtbTrzzlmVX3Cl1o/ https://docs.google.com/document/d/1dQaDipbs3BkMYcHx_1s6qFjyltmLtbTrzzlmVX3Cl1o/
From your following script in your question and your sample input and output situations,从您的问题中的以下脚本以及您的示例输入和 output 情况,
demo_content = ss.getRangeByName("SectionContentsDemo").getValue().toString().replace(/\n/g,'\r\n'),
template_doc_body.replaceText("{{Section Contents - Demo}}", demo_content);
I thought that when the value including the line break is replaced with {{Section Contents - Demo}}
in your Google Document, the indent might be changed after 2nd line.我认为当包含换行符的值在您的 Google 文档中被替换为{{Section Contents - Demo}}
时,缩进可能会在第 2 行之后更改。 So, in this case, how about using Docs API?那么,在这种情况下,使用文档 API 怎么样? When the Docs API is used, a sample script is as follows.使用Docs API时,示例脚本如下。
Before you use this script, please enable Docs API at Advanced Google services .在使用此脚本之前, 请在 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
of Spreadsheet, and the retrieved value is replaced with {{Section Contents - Demo}}
in the document.运行此脚本时,将从电子表格的SectionContentsDemo
的命名范围中检索该值,并将检索到的值替换为文档中的{{Section Contents - Demo}}
。 In this case, it seems that the indent is followed to {{Section Contents - Demo}}
.在这种情况下,似乎缩进跟在{{Section Contents - Demo}}
之后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.