简体   繁体   English

Google Spreadsheet-根据另一个单元格的内容将日期添加到单元格

[英]Google Spreadsheet - add Date to cell depending upon the contents of another cell

I have the following code to check column A for a certain value. 我有以下代码来检查列A的某个值。 (in this case 'Work in progress') The date is added to the adjacent cell in column B. (在这种情况下为“进行中”)日期被添加到B列中的相邻单元格中。

What I want is that this only happens when the adjacent cell is empty. 我想要的是,仅当相邻单元格为空时才会发生这种情况。 If a date is already in that cell, it needs to skip adding a new date. 如果该单元格中已有日期,则需要跳过添加新日期的操作。

function onEdit(e) {
    // Work in progress Begin Date  
    var colToWatch = 1, colToStamp = 2;
    var valueToWatch = "Work in progress"; 
    if (e.range.columnStart === colToWatch && (e.value === valueToWatch || typeof e.value == 'object'))
        e.source.getActiveSheet()
            .getRange(e.range.rowStart, colToStamp)
            .setValue(typeof e.value === 'object' ? null : new Date());
}

Using Apps Script with a Google Spreadsheet you can run some code when a cell is edited, and then check the column and value of the cell, and then either enter the date or not. 将Apps Script与Google Spreadsheet结合使用时,可以在编辑单元格时运行一些代码,然后检查单元格的列和值,然后输入日期或不输入日期。

function onEdit(e) {//The letter "e" is used to get the event object
  var adjacentCell,rng,row,sh;//define variables without assigning a value

  var colToWatch = 1, colToStamp = 2;
  var valueToWatch = "Work in progress"; 

  rng = e.range;
  row = rng.getRow();

  if (rng.getColumn() !== colToWatch || e.value !== valueToWatch) {return;}

  sh = e.source.getActiveSheet();

  adjacentCell = sh.getRange(row,colToStamp).getValue();
  Logger.log('adjacentCell: ' + adjacentCell)
  Logger.log(!adjacentCell)

  if (adjacentCell) {return;}//If there is a value in cell then stop

  sh.getRange(row, colToStamp)
    .setValue(new Date());

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google表格:“单击”时,用另一个单元格内容替换单元格内容吗? - Google Sheets: Replace cell contents with another cell contents when “Clicked”? 使用Javascript中的if语句确定Google电子表格中的单元格是否具有内容 - Use an if statement in Javascript to determine if a cell in a google spreadsheet has contents 从Google电子表格检索单元格日期到HTML服务 - Retrieve cell date from a google spreadsheet to HTML service 如果另一个单元格不为空,如何为单元格设置代码Google SpreadSheet - How to set a code to a cell if another isn't null Google SpreadSheet Google电子表格脚本从单元格读取时将逗号更改为句号? - google spreadsheet script changes comma to full-stop upon reading from a cell? 在两列的每一行中的每个单元格中添加1,直到到达空白单元格-Google Spreadsheet - Add 1 to each cell in every row of two columns until a blank cell is reached - Google Spreadsheet Google电子表格脚本,该脚本会在工作表中的列的编辑时触发,并根据值覆盖用户定义的单元格的值 - A google spreadsheet script that triggers on edit of a column in a sheet and overwrites the value of a user defined cell or not depending on the value 根据另一个单元格的内容清除一个单元格 - clear a cell based on the contents of another cell 如何根据ext js网格中另一个单元格的值向单元格添加类 - How to add class to a cell depending on the value of another cell in ext js grid 将值分配给Google电子表格单元格时出错 - error in assigning values to google spreadsheet cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM