Google apps script 将数据行从一个工作表移动到另一个工作表-GoogleSheet
我正试图根据Action列中的触发字“order”将一行数据从一个工作表(IT库存)移动到另一个工作表(库存分配)。当数字变为0时,“操作”列将更改为“顺序”…此操作有效: 但我也希望整行移到第二张工作表(InventoryAllocation),并将其从第一张工作表中删除 我的问题是,我应该在脚本中更改哪些变量/声明,因为我已经尝试了各种变量/声明,并且不断出现错误: TypeError:无法从未定义中读取属性“source”。(第6行,文件“代码”) 这是我正在使用的脚本:Google apps script 将数据行从一个工作表移动到另一个工作表-GoogleSheet,google-apps-script,Google Apps Script,我正试图根据Action列中的触发字“order”将一行数据从一个工作表(IT库存)移动到另一个工作表(库存分配)。当数字变为0时,“操作”列将更改为“顺序”…此操作有效: 但我也希望整行移到第二张工作表(InventoryAllocation),并将其从第一张工作表中删除 我的问题是,我应该在脚本中更改哪些变量/声明,因为我已经尝试了各种变量/声明,并且不断出现错误: TypeError:无法从未定义中读取属性“source”。(第6行,文件“代码”) 这是我正在使用的脚本: /** *
/**
* Moves row of data to another spreadsheet based on criteria in column 5 to sheet with same name as the value in column 6.
*/
function onEdit(e) {
var ss = e.source;
var s = ss.getActiveSheet();
var r = e.range;
// The code below logs the ID for the active spreadsheet.
Logger.log(SpreadsheetApp.getActiveSpreadsheet().getId());
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 5;
var nameCol = 6;
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if the action/status col is changed to ok do stuff. Change "ok" below to whatever you want the trigger word to be.
if (e.value == "ORDER" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the Skin Type column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) {
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.'
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after. Put // in front of below row if you don't want to delete after copying to new sheet
s.deleteRow(rowIndex);
// or remove // below if you want to keep but note the move, but you'll need to add // to row above if removing // from below
// r.setValue("Copied");
}
}
}
任何帮助都将不胜感激
谢谢 根据错误,您运行此函数时似乎没有生成onEdit触发器。您说“当数字变为0时,操作列将更改为顺序”,因此我假设您有一些单元格公式正在一起工作以更改“操作列”。然而,问题是只有用户操作才会生成编辑事件。因此,通过某些单元格公式更改单元格的值永远不会在onEdit触发器上生成。您无法从代码编辑器运行此函数。它必须通过编辑单元格来运行。是的,这就是我希望发生的事情;编辑单元格时(输入值为0),脚本将运行…将包含已编辑单元格的行移动到第二张工作表。