【问题标题】:Edit script to apply across all sheets编辑脚本以应用于所有工作表
【发布时间】:2019-05-05 12:37:03
【问题描述】:

这是我目前正在使用的脚本。我希望它自动应用于我文档中的所有工作表。请帮忙。以下是代码。

/**
* Creates a Date Stamp if a column is edited.
*/

//CORE VARIABLES
// The column you want to check if something is entered.
var COLUMNTOCHECK = 1;
// Where you want the date time stamp offset from the input location. [row, column]
var DATETIMELOCATION = [0,1];
// Sheet you are working on
var SHEETNAME = '46'

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  //checks that we're on the correct sheet.
  if( sheet.getSheetName() == SHEETNAME ) { 
    var selectedCell = ss.getActiveCell();
    //checks the column to ensure it is on the one we want to cause the date to appear.
    if( selectedCell.getColumn() == COLUMNTOCHECK) { 
      var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
      dateTimeCell.setValue(new Date());
      }
  }
}

我希望脚本在每个工作表上执行,这样每当我添加新工作表时,脚本就会自动运行,并在我向第 1 列添加值时帮助我向第 2 列添加时间戳。

请帮忙。

我在这里完全是菜鸟,因此一些详细的解释将对我有很大帮助。

【问题讨论】:

    标签: javascript google-sheets


    【解决方案1】:

    基本上,只需删除if 语句,代码将应用于每张纸。

    在下面给出的答案中,我还使用了 onEdit 触发器返回的一些对象。


    /**
    * Creates a Date Stamp if a column is edited.
    */
    
    //CORE VARIABLE
    // The column you want to check if something is entered.
    var COLUMNTOCHECK = 1;
    
    function onEdit(e) {
      var sh = e.range.getSheet();
      if (e.range.columnStart == COLUMNTOCHECK){
        // edited the right column do something
        var targetcell = sh.getRange(e.range.rowStart,2);
        targetcell.setValue(new Date());
      }
      else {
      // do nothing
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2015-11-29
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      相关资源
      最近更新 更多