【问题标题】:Google Script get a text from a cell(Spredsheet special characters like ç, à,é,è ) and search for it in a Google Doc documentGoogle Script 从单元格中获取文本(Spredsheet 特殊字符,如 ç、à、é、è)并在 Google Doc 文档中搜索
【发布时间】:2018-07-27 18:19:51
【问题描述】:

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

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

【问题讨论】:

    标签: regex google-apps-script google-sheets google-docs


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 1970-01-01
      • 2013-07-20
      • 2020-05-05
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 2012-04-26
      • 2020-10-01
      相关资源
      最近更新 更多