Google Apps 脚本 - 使用 .replace 方法删除空格
Google Apps Script - remove spaces using .replace method
我正在尝试使用脚本将 Google 文档中的多个空格替换为单个空格。但不幸的是,提供的解决方案 to this question 对我不起作用。如您所见,我尝试了几种替代方法,但无法弄清楚如何正确执行。有什么想法吗?
function searchAndReplace() {
var body = DocumentApp.getActiveDocument()
.getBody();
body.replaceText(/\s{2,}/,' ');
body.replaceText(/\s/g, " ") ;
body.replaceText("/\s/"," ");
body.replaceText('/\s{2,}/',' ');
}
尝试:
function searchAndReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.editAsText().replaceText('\s*', ' ');
}
更新
一个选项是:
function getCorrections() {
var _getCorrections = 0,
text = DocumentApp.getActiveDocument().getBody().getText(),
regexp = /\s+/g,
matchesCorrections = text.match(regexp);
if (matchesCorrections) {
_getCorrections = matchesCorrections.reduce(function(previousValue,
currentValue) {
return previousValue + currentValue.length - 1;
}, 0);
}
return _getCorrections;
}
我正在尝试使用脚本将 Google 文档中的多个空格替换为单个空格。但不幸的是,提供的解决方案 to this question 对我不起作用。如您所见,我尝试了几种替代方法,但无法弄清楚如何正确执行。有什么想法吗?
function searchAndReplace() {
var body = DocumentApp.getActiveDocument()
.getBody();
body.replaceText(/\s{2,}/,' ');
body.replaceText(/\s/g, " ") ;
body.replaceText("/\s/"," ");
body.replaceText('/\s{2,}/',' ');
}
尝试:
function searchAndReplace() {
var body = DocumentApp.getActiveDocument().getBody();
body.editAsText().replaceText('\s*', ' ');
}
更新
一个选项是:
function getCorrections() {
var _getCorrections = 0,
text = DocumentApp.getActiveDocument().getBody().getText(),
regexp = /\s+/g,
matchesCorrections = text.match(regexp);
if (matchesCorrections) {
_getCorrections = matchesCorrections.reduce(function(previousValue,
currentValue) {
return previousValue + currentValue.length - 1;
}, 0);
}
return _getCorrections;
}