Google 脚本从单元格中获取文本(Spredsheet 特殊字符,如 ç、à、é、è )并在 Google Doc 文档中搜索它

Google Script get a text from a cell(Spredsheet special characters like ç, à,é,è ) and search for it in a Google Doc document

我在我的电子表格中做了一个脚本(容器绑定脚本),其中有 4 列:(1) 之前的文本,(2) 之后的文本,(3) 要插入的文本,以及 (4) a Google 文档的 URL,我想用正确的值(之间)替换其中的文本。

当我使用法语文本(带有 ç 、 à 、 è 等字符)但使用英文文本时,我的替换方法不起作用 如何解决这个问题?非常感谢您的帮助,欢迎任何想法这是我到目前为止所做的 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 ); 
}

您不需要使用替换。在这种情况下,只需 \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 );