通过 Google Apps 脚本删除范围 (\n) 中单元格内的换行符

Wtf*_*EJw 5 google-spreadsheets

在 Google 电子表格中,我通常会执行以下操作:

  1. 转到查找和替换
  2. 检查“正则表达式”
  3. 查找\n并替换为null

我进行了搜索,但我一生都无法找到可以用来通过代码完成此过程的 Googl Apps 脚本。

小智 3

var ui = SpreadsheetApp.getUi();

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Automation')
      .addItem('Delete Enters', 'deleteEnters')
      .addToUi();
}

function deleteEnters() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  if (!sheet){
    ui.alert("There is no sheet with config!"); 
  }

  var lastColumn = sheet.getLastColumn();
  var lastRow = sheet.getLastRow();

  for (i = 1; i <= lastColumn; i++) {
    for (j = 1; j <= lastRow; j++) {
      var tempText = sheet.getRange(j,i).getValue();
      tempText = tempText.replace(/\n/g,"");
      sheet.getRange(j, i).setValue(tempText);
    }
  }  
}
Run Code Online (Sandbox Code Playgroud)

你好,我为你做了这个简单的代码。因此,您可以在当前工作表选项“自动化”>“删除输入”中选择,脚本会自动将 \n 替换为“”

您可以更改此行:

tempText = tempText.replace(/\n/g,"");
Run Code Online (Sandbox Code Playgroud)

对于您自己的正则表达式,如果您想替换其他内容的输入。