简体   繁体   English

Google Apps 脚本:将软换行符替换为硬换行符

[英]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时,示例脚本如下。

Sample script:示例脚本:

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);
}
  • When this script is run, the value is retrieved from the named range of 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}}之后。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps脚本中的强行换行(带有Google Form字段的Google文档可在项目符号列表中创建新项目符号) - Hard Line Break in Google Apps Script (Google Docs with Google Form Field to create new bullet in bulleted list) 正则表达式替换 function 不适用于 Apps 脚本中字符串末尾的换行符 - REGEX replace function not working for line break at the end of the string in Apps Script 用换行符替换字符 - Replace a character by a line break 如何在文本区域中将换行符替换为换行符 - How to replace word break with line break in a textarea 如何替换文本区域中的换行符 - How to replace the line break in a textarea JavaScript 用文字字符串换行符替换比喻字符串换行符 - JavaScript replace figurative string line break with literal string line break 下划线用换行符替换新的换行符 - Underscore replace new line characters with line break 如何中断一行Java脚本 - how to break a line java script 如何将应用脚本中的换行符添加到 Google 表格 - How To Add Break Line From App Script To Google Sheet 来自api响应的字符串不能显示断线,但硬代码字符串可以显示断线 - String from api response cant display break line, but hard code string can display break line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM