仅在一列

时间:2016-01-12 16:09:03

标签: events google-apps-script google-sheets edit

我目前正在使用此脚本:

function onEdit(e)

  // Set a comment on the edited cell to indicate when it was changed.
  var range = e.range;
  range.setNote('Laatst veranderd: ' + new Date());

我需要添加什么才能在列C'?'

中使用

3 个答案:

答案 0 :(得分:2)

如果编辑了某个列,则限制代码在Google表格中运行。这使用了Apps脚本onEdit()保留的函数名称,该名称被触发以在编辑事件上运行。

获取范围的列号:

function onEdit(e) {//"e" receives the event object
  var range = e.range;//The range of cells edited

  var columnOfCellEdited = range.getColumn();//Get column number
  //Logger.log(columnOfCellEdited)

  if (columnOfCellEdited === 3) {// Column 3 is Column C
    //Set a comment on the edited cell to indicate when it was changed.
    range.setNote('Laatst veranderd: ' + new Date());
  };
};

另一个版本:

function onEdit(e) {//"e" receives the event object
  var range = e.range;//The range of cells edited

  var columnOfCellEdited = range.getColumn();//Get column number
  //Logger.log(columnOfCellEdited)


  if (columnOfCellEdited !== 3) {return;}// Halt the code if the column 
    //edited is not column C
    //Set a comment on the edited cell to indicate when it was changed.

  range.setNote('Laatst veranderd: ' + new Date());

};

答案 1 :(得分:0)

您也可以尝试提取列索引。函数getA1Notation()返回可用于解析列的单元格位置。

function onEdit(e){
  // Set a comment on the edited cell to indicate when it was changed.
  var range = e.range;
  var title = range.getA1Notation();
  var data = {
   'bookName': 'Some book',
   'whoAdded': 'Nick',
   'whenAdded': new Date()
 };
 var options = {
   'method' : 'post',
   'contentType': 'application/json',
   // Convert the JavaScript object to a JSON string.
   'payload' : JSON.stringify(data)
 };
  if(title.charAt(0) === 'B'){
      var result = UrlFetchApp.fetch('https://xxx.xxx', options);
  }else{
    range.setNote('Failed to upload request :(');
  }
}

答案 2 :(得分:-2)

如果添加这些{},它应适用于任何列:

function onEdit(e) {
//Set a comment on the edited cell to indicate when it was changed.
var range = e.range;
range.setNote('Laatst veranderd: ' + new Date());
}