我在电子表格中做了一个脚本(包含容器的脚本),其中有4列:(1)之前的文本,(2)后面的文本,(3)要在其间插入的文本和(4)URL为包含我要在其中替换为正确值的文本的Google文档。
当我使用法语文本(带有ç,à,è)时,我的方法替换不起作用,但是使用英语文本则可以正常工作如何解决此问题?非常感谢您的帮助,欢迎任何想法,这是我到目前为止https://drive.google.com/drive/folders/1dOVNMrzEHvi3-vU3nbftK3Xoinxscrkn和我的代码:
/** It works for a text without accents :) but not for a french text :( **/
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Update the Google Doc") ;
var lastColumn = sheet.getLastColumn();
var numRows = sheet.getLastRow();
var COLUMN_URL = 3 ;
var data = sheet.getRange(1,1,numRows,lastColumn).getValues();
var start = 1;
var URL = data[start][COLUMN_URL];
Logger.log(' URL ' + URL);
var body = DocumentApp.openByUrl(URL).getBody();
var text_before = sheet.getRange(start + 1,1).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_before is " + text_before );
var text_after = sheet.getRange(start + 1,2).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_after is " + text_after );
var text_between = sheet.getRange(start + 1,3).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_between is " + text_between );
/** replace in the body of the Google Doc **/
// important to do this for the apostrophe and the " symbols that are different put the symbol in the cell
body.replaceText("\\Q’\\E","'");
// works
body.replaceText("\\Q”\\E",'"')
// ???? replace all unsupported characters from sheet means in my cell
/** symbols to test which works >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ok for the McDonald*?()\.,;%#(){!s . how about the " ***/
body.replaceText( "\\Q" + text_before + "\\E" + ".*?" + "\\Q" + text_after + "\\E", text_before + text_between + text_after );
/** another example **/
var start_bis = 2;
var text_before_bis = sheet.getRange(start_bis + 1,1).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_before is " + text_before_bis );
var text_after_bis = sheet.getRange(start_bis + 1,2).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_after is " + text_after_bis );
var text_between_bis = sheet.getRange(start_bis + 1,3).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_between is " + text_between_bis );
/** replace in the body of the Google Doc **/
body.replaceText( "\\Q" + text_before_bis + "\\E" + ".*?" + "\\Q" + text_after_bis + "\\E", text_before_bis + text_between_bis + text_after_bis );
}
答案 0 :(得分:1)
您不需要使用replace。在这种情况下,只需\\Q...\\E
即可正常工作。替换后,.
由于(QE)而被视为文字文本。因此,它没有用。
尝试
/** It works for a text without accents :) but not for a french text :( **/
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Update the Google Doc") ;
var lastColumn = sheet.getLastColumn();
var numRows = sheet.getLastRow();
var COLUMN_URL = 3 ;
var data = sheet.getRange(1,1,numRows,lastColumn).getValues();
var start = 1;
var URL = data[start][COLUMN_URL];
Logger.log(' URL ' + URL);
var body = DocumentApp.openByUrl(URL).getBody();
var text_before = sheet.getRange(start + 1,1).getDisplayValue();
Logger.log("text_before is " + text_before );
var text_after = sheet.getRange(start + 1,2).getDisplayValue();
Logger.log("text_after is " + text_after );
var text_between = sheet.getRange(start + 1,3).getDisplayValue();
Logger.log("text_between is " + text_between );
/** replace in the body of the Google Doc **/
// important to do this for the apostrophe and the " symbols that are different put the symbol in the cell
body.replaceText("\\Q’\\E","'");
// works
body.replaceText("\\Q”\\E",'"')
// ???? replace all unsupported characters from sheet means in my cell
/** symbols to test which works >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ok for the McDonald*?()\.,;%#(){!s . how about the " ***/
body.replaceText( "\\Q" + text_before + "\\E" + ".*?" + "\\Q" + text_after + "\\E", text_before + text_between + text_after );