Google表格根据值和编辑值移动行

时间:2018-12-18 09:13:59

标签: google-apps-script google-sheets

我一直在Stack上阅读答案,并最终决定加入!所以这是我的第一个问题:

我在这里找到了一个针对Google工作表的Google Apps脚本,该脚本根据单元格的值将一行移动到另一工作表。这很棒。我已经对其进行了测试,并且它通过以下更改对我有用:首先使用复选框,其次使用复选框复制除第一列之外的所有列。

但是,为了对我真正有用,我希望能够在将行添加到新工作表之前编辑几个值。

所以会是这样:

  1. 如果Checkbox = true,则复制行,除复选框外的所有列
  2. 将“位置”的值设置为“伦敦”(例如第8列)
  3. 将“ date”的值设置为今天(例如第12列)
  4. 将已编辑的行添加到底部的新工作表中

有人可以帮助我实现这一目标吗?我猜它一定很简单,但是我是应用脚本的新手,所以不知道该怎么做。

以下是移动我从2011年帖子中找到的行的基本脚本:

function onEdit(event) {
  // assumes source data in sheet named Needed
  // target sheet of move to named Acquired
  // test column with checkbox is col 1 or A
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Needed" && r.getColumn() == 1 && r.getValue() == true) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Acquired");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 2, 1, numColumns).copyTo(target);
  }
}

希望有人可以提供帮助。预先感谢!

2 个答案:

答案 0 :(得分:0)

请尝试此代码,

function onEdit(event) {
  // assumes source data in sheet named Needed
  // target sheet of move to named Acquired
  // test column with checkbox is col 1 or A
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  if(s.getName() == "Needed" && r.getColumn() == 1 && r.getValue() == true) {
    var row = r.getRow();
    var numColumns = s.getLastColumn() - 1;
    var data = s.getRange(row, 2, 1, numColumns).getValues();
    data[0][8] = 'London';
    data[0][12] = new Date();
    var targetSheet = ss.getSheetByName("Acquired");
    targetSheet.getRange(targetSheet.getLastRow() + 1, 1, 1, numColumns).setValues(data);
  }
}

答案 1 :(得分:0)

请尝试一下,让我知道你的情况。 :)

function onEdit(event) {
//assumes source data in sheet named Needed
//target sheet of move to named Acquired
//test column with checkbox is col 1 or A
var spreadsheet = event.source;
var sheet = spreadsheet .getActiveSheet();
var range = spreadsheet.getActiveRange();
var sheetName = sheet.getName();
var col = range.getColumn();
var value = range.getValue();

if (sheetName === "Needed" && col === 1 && value === true) {
var row = range.getRow();
var numColumns = sheet.getLastColumn();
var targetSheet = spreadsheet.getSheetByName("Acquired");
var lastRow = targetSheet.getLastRow();
var target = targetSheet.getRange(lastRow + 1, 1, 1, numColumns - 1);
//added number of rows and columns to select a range instead of a single cell
var values = sheet.getRange(row, 2, 1, numColumns - 1).getValues();

values[0][7] = values [0][1];
//access value of row 1, column 8 and set to value of row 1, column 2
//the values are captured in an array and are zero-based. The first column is column 0 for example.

values[0][11] = new Date();
//access value of row 1, column 12 and set to date-time value of 'now'.

target.setValues(values);
}
}