[英]script to copy a row of data from one sheet to a specific sheet based on the value of a cell
I'm trying to create a script to copy a row of data from one sheet to a another sheet based on the value of a cell.我正在尝试创建一个脚本来根据单元格的值将一行数据从一个工作表复制到另一个工作表。
But essentially when a new row of information is input in the main sheet it'll copy the row and place that row of data into a another sheet based off the value of a specific cell.但本质上,当在主工作表中输入新的信息行时,它将复制该行并将该行数据放置到基于特定单元格的值的另一个工作表中。 In this case it's the utility company.在这种情况下,它是公用事业公司。 If, for example, the customer is created and the utility company they have is United Power I want to have the sheet copy that customers information over and add it to the specified United Power sheet and so on and so forth with the other utility company's (3 total)例如,如果创建了客户并且他们拥有的公用事业公司是 United Power 我希望拥有客户信息的表格副本并将其添加到指定的 United Power 表格中,依此类推,其他公用事业公司的 (共 3 个)
I keep getting the below error: TypeError: Cannot read property 'source' of undefined (line 4, file "Code")我不断收到以下错误:TypeError:无法读取未定义的属性“源”(第 4 行,文件“代码”)
I'll paste below what I have so far.我将粘贴到目前为止。 (I only have the code for one so far) I can also link the spreadsheet if need be. (到目前为止,我只有一个的代码)如果需要,我还可以链接电子表格。
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Contracts" && r.getColumn() == S && r.getValue() == "United Power") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("United Power");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
}
}
You could have written like this:你可以这样写:
You need to change S to a number您需要将 S 更改为数字
function onEdit(e) {
const sh=e.range.getSheet();
if(sh.getName()=="Contracts" && e.range.columnStart==19 && e.value=="United Power") {
const tsh = e.source.getSheetByName("United Power");
const target = tsh.getRange(tsh.getLastRow() + 1, 1);
sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()).moveTo(target);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.