如何在Google Doc应用程序脚本中替换换行符或换行符(Ctrl + Enter)? [英] How to replace newline or soft linebreak (ctrl+enter) in Google doc app script?
问题描述
我有一个有效的模板(Google文档),并且具有以下模式的变量要替换为值
I have a working template (Google Doc) and have variables with following patterns to be replace with values
{{BASIC SALARY_.Description}}
{{OT1.5.Description}}
{{MEL ALW.Description}}
{{OST ALW.Description}}
{{TRV ALW.Description}}
{{ADV SAL.Description}}
{{BASIC SALARY_.Description}}
{{OT1.5.Description}}
{{MEL ALW.Description}}
{{OST ALW.Description}}
{{TRV ALW.Description}}
{{ADV SAL.Description}}
注意:我在Google文档中使用了软换行符(ctrl + enter),因为我无法找出正常的换行符"\ n","\ n","\ r \ n",但我的结果总是很奇怪,因为某些行需要替换为适当的描述,但有些行需要完全无效(将整个{{pattern}}与换行符一起删除以避免空行)
note: I am using soft line break (ctrl+enter) in google doc as I couldn't figure out to detect normal linebreak pattern "\n", "\n", "\r\n" but my result always weird as some line need to be replaced as proper descriptions but some need to be totally nullify (remove whole {{pattern}} together with the line break to avoid empty line)
我已经尝试了多种REGEX模式,在在线论坛上搜索了
使用regex消除google app脚本中的换行符
在Google Doc Apps脚本中使用RegEx替换文本
I have tried out multiple REGEX patterns, googled the online forum
https://github.com/google/re2/wiki/Syntax
Eliminate newlines in google app script using regex
Use RegEx in Google Doc Apps Script to Replace Text
找出唯一的软换行符是唯一的处理方法(确定模式 \ v .请检查我的示例代码,因为模式替换无法正常工作.
and figure out only soft linebreak is the only way to deal with (identify pattern \v. Please check my sample code as the pattern replace doesn't work as expected.
// code block 1
var doc = DocumentApp.openById(flPayslip.getId());
var body = doc.getBody();
body.replaceText("{{BASIC SALARY_.Description}}", "Basic Salary");
body.replaceText("{{OST ALW.Description}}", "Outstation Allowance");
// code block 2
var doc = DocumentApp.openById(flPayslip.getId());
var body = doc.getBody();
body.replaceText("{{BASIC SALARY_.Description}}", "Basic Salary");
body.replaceText("{{OST ALW.Description}}", "Outstation Allowance");
body.replaceText("{{.*}}\\v+", ""); // to replace soft linebreak
代码块1的实际结果
Actual Result of code block 1
基本工资
{{OT1.5.Description}}
{{MEL ALW.Description}}
离站津贴
{{TRV ALW.Description}}
{{ADV SAL.Description}}
Basic Salary
{{OT1.5.Description}}
{{MEL ALW.Description}}
Outstation Allowance
{{TRV ALW.Description}}
{{ADV SAL.Description}}
代码块2的实际结果:
基本工资
Basic Salary
问题:实际结果"Outstation Allowance"已从正则表达式替换中删除.
Issue: actual result "Outstation Allowance" was removed from regex replacement.
预期结果
基本工资
离站津贴
Basic Salary
Outstation Allowance
我应该在代码中使用什么合适的正则表达式模式?
What's the proper regex pattern I should use in my code?
推荐答案
尝试
body.replaceText("{{[^\\v]*?}}\\v+", ""); // No \v inside `{{}}` and not greedy`?`
使用{{.*}}
时,.*
匹配第一个{{
和最后一个}}
When you use {{.*}}
, .*
matches everything between the first {{
and the last }}
基本工资
{{
Basic Salary
{{
OT1.5.Description}}
{{MEL ALW.Description}}
离站津贴
{{TRV ALW.Description}}
{{ADV SAL.说明
OT1.5.Description}}
{{MEL ALW.Description}}
Outstation Allowance
{{TRV ALW.Description}}
{{ADV SAL.Description
}}
这篇关于如何在Google Doc应用程序脚本中替换换行符或换行符(Ctrl + Enter)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!