[英]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.