Google表格脚本用单元格中的值替换空单元格

时间:2019-11-21 11:05:20

标签: google-apps-script google-sheets

如果单元格A2:A100中的单元格为空,我想用onEdit触发一个触发器,并将其替换为单元格B1中公式结果的值 我的编码如下,但仅替换为脚本“ HELLO”上设置的值 我需要使用单元格B1中的值更改“ HELLO”

function onEdit(e) {
 var spreadsheet = SpreadsheetApp.getActive();
var ss = SpreadsheetApp.getActive()
    .getSheetByName('Feuille 1');
var range = ss.getRange("A2:A15")
    .offset(0, 0, ss.getDataRange()
         .getNumRows());
range.setValues(range.getValues()
    .map(function (row) {
        return row.map(function (cell) {
            return cell === '' ? 'HELLO'   : cell;
        });
    }));
}

https://i.stack.imgur.com/k4Lku.jpg

1 个答案:

答案 0 :(得分:0)

将代码更改为:

function onEdit(e) {
  if (e.range.getColumn() == 1)  { //Column A was edited
    if (e.value == '' || e.value == null) { //And the cell was set to BLANK
      //Then I should set this cell to 'B1' 
      var B1Value = SpreadsheetApp.getActiveSheet().getRange("B1").getValue();
      e.range.setValue(B1Value);
    }
  }
}