0

I am working on a script for a Google sheet and learning scripts from scratch.

I've used the following script and changed the input column with success, but the output remains in column A. I want the output to go to column T and the row should remain the same.

The trigger type is good but it works in all the sheets so I need to limit the sheets it works in. How do I make the script work in one particular sheet and tab combination?

As long as I am asking about changing the output to the one column, can you explain the general method for determining the output column and row?

Thanks in advance.

Code, here is the exact script I am using. Once I Run the script in the editor it gives me an error on line 3 which is "var eventRange=event.range;"

that stops if i remove the quotataions from around the sheet name 'ticket to deliver' in line 6. but i get a new error when I run the script on line 6 "which says "missing ) after condition, line 6". Here is the code I am working with which was written by another poster here.

function onEdit(event) {

  var eventRange = event.range;
  var sheetName = eventRange.getSheet().getName();

  if (eventRange.getColumn() == 14 && sheetName == 'tickets to deliver') {

//if (eventRange.getColumn() == 14 && sheetName.match(/sheetName1|sheetName2/)) {

    // getRange(row, column, numRows, numColumns)
    var columnXRange = SpreadsheetApp.getActiveSheet().getRange(eventRange.getRow(), 21, eventRange.getNumRows(), 1);
    //second parameter changed from '1' to '21'. 21 represents column 'U'

    var values = columnXRange.getValues();

    for (var i = 0; i < values.length; i++) {
      if (!values[i][0]) {
        values[i][0] = new Date();
      }
    }
    columnXRange.setValues(values);
  }
}
4

2 回答 2

1

我希望输出转到 T 列,并且该行应该保持不变。

更改, 1,, 20,

于 2018-12-31T06:34:36.943 回答
0

您可以通过检查工作表名称来管理脚本影响的工作表。下面有两个选项。

  1. 第一个是将工作表名称与字符串完全匹配。
  2. 第二个(注释掉)是将工作表的名称与正则表达式匹配。

从 A 到 T 的列调整在下面评论。

function onEdit(event) {

  var eventRange = event.range;
  var sheetName = eventRange.getSheet().getName();

  if (eventRange.getColumn() == 14 && sheetName == 'yourSheetNameHere') {

//if (eventRange.getColumn() == 14 && sheetName.match(/sheetName1|sheetName2/)) {

    // getRange(row, column, numRows, numColumns)
    var columnXRange = SpreadsheetApp.getActiveSheet().getRange(eventRange.getRow(), 20, eventRange.getNumRows(), 1);
    //second parameter changed from '1' to '20'. 20 represents column 'T'

    var values = columnXRange.getValues();

    for (var i = 0; i < values.length; i++) {
      if (!values[i][0]) {
        values[i][0] = new Date();
      }
    }
    columnXRange.setValues(values);
  }
}

编辑

谈话结束后,我跳上电脑测试脚本。

这是我的工作脚本。(刚刚删除了评论)实际脚本应该没有变化。

另外,请注意,在测试需要事件对象的 onEdit 或 onChange 函数时,不能只按原样使用调试函数。如果要使用调试,在此函数中,您需要将第 3 行更改为var eventRange = SpreadsheetApp.getActive.getActiveRange();.

另请注意,工作表名称测试字符串需要准确且区分大小写。

工作表名称的片段

function onEdit(event) {

  var eventRange = event.range;
  var sheetName = eventRange.getSheet().getName();

  if (eventRange.getColumn() == 14 && sheetName == 'tickets to deliver') {

    var columnXRange = SpreadsheetApp.getActiveSheet().getRange(eventRange.getRow(), 21, eventRange.getNumRows(), 1);

    var values = columnXRange.getValues();

    for (var i = 0; i < values.length; i++) {
      if (!values[i][0]) {
        values[i][0] = new Date();
      }
    }
    columnXRange.setValues(values);
  }
}
于 2018-12-31T14:02:52.040 回答