检查是否存在单元格注释

时间:2016-10-03 21:02:06

标签: google-apps-script google-sheets

我使用以下脚本将输入的值和时间戳作为单元格注释添加到相邻单元格,然后清除输入值的单元格:

switch (comments) {
  case  "":
    comments = "(" + timestamp + ") " + cellValue;
    adjacent.setComment(comments);
    changedRange.clearContent();
    break;
  default:
    comments = comments + "\n" + "(" + timestamp + ") " + cellValue;
    adjacent.setComment(comments);
    changedRange.clearContent();
    break;}

它工作正常,但它每次只运行默认情况,因为脚本没有看到无记录的单元格(case"" :)。这不是一个大问题,但我希望当音符添加到无音符单元格时,音符的第一行上方没有空格。我也尝试使用if then else语句,但无法使其工作

if (comments == "") {
     comments = "(" + timestamp + ") " + cellValue;
     adjacent.setComment(comments);
     changedRange.clearContent(); }
  else if (comments != "") {
     comments = comments + "\n" + "(" + timestamp + ") " + cellValue;
     adjacent.setComment(comments);
     changedRange.clearContent(); }

有更好的方法可以做到这一点或检查无记录细胞吗?感谢

1 个答案:

答案 0 :(得分:0)

第二位代码就是答案。完整的代码:

function revTime(event){
  var ColCR = 96;  // Column Number of "CR"

  var changedRange = event.source.getActiveRange();
  if (changedRange.getColumn() == ColCR) {
    // An edit has occurred in Column CR
    var state = changedRange.getValue();
    var adjacent = event.source.getActiveSheet().getRange(changedRange.getRow(),ColCR+1);
    var comments = adjacent.getComments();
    var timestamp = Utilities.formatDate(new Date(), "GMT-7", "M/dd/yy', 'h:mm a"); // Get the current time
    // We'll do something different, depending on the selected value
     if (comments == "") {
     comments = "(" + timestamp + ") " + state;
     adjacent.setComment(comments);
     changedRange.clearContent(); }
    else if (comments != "") {
     comments = comments + "\n" + "(" + timestamp + ") " + state;
     adjacent.setComment(comments);
     changedRange.clearContent(); } 
  }
}