我第一次尝试编写脚本。我正在尝试创建一个库存跟踪器。我在Here找到了一个很好的讨论和脚本,但我需要进行一些小修改。该脚本使用"转储"这将是我的"购买"工作簿。将项目输入转储页面时,脚本会根据"值"确定项目的转移位置。 col 1和col 2。
我希望在不需要条件的情况下根据项目传输数据。我目前的专栏是: 日期,项目,供应商,数量,成本
来自链接的代码在
之下
var fedId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var fedSheetName = 'Sheet1';
var dhlId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var dhlSheetName = 'Sheet1';
var upsId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var upsSheetName = 'Sheet1';
var dumpSheetName = 'Sheet1';
var firstColToWatch = 2;
var secondColToWatch = 1;
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Transfer')
.addItem('Transfer rows', 'moveRows')
.addToUi();
}
function moveRows() {
var s = SpreadsheetApp.getActive()
.getActiveSheet();
var rows = [];
var writeBack = [];
var range = s.getDataRange();
var val = range.getValues()
.forEach(function (r, i) {
if (r[1].substring(0, 3) == "DHL" && r[0] == 'Watches') { //&& is used because both conditions need to evaluate to 'true'
moveToSheet(dhlId, dhlSheetName, r);// Javascript arrays are zero-indexed. So r[0] is column 1...
rows.push(i + 1);
} else if (r[1].substring(0, 3) == "DHL" && r[0] == 'Shirts') {
moveToSheet(dhlId, dhlSheetName, r);
rows.push(i + 1);
} else if (r[1].substring(0, 3) == "UPS" && r[0] == 'Shoes') {
moveToSheet(upsId, upsSheetName, r);
rows.push(i + 1);
} else if (r[1].substring(0, 3) == "UPS" && r[0] == 'Pants') {
moveToSheet(upsId, upsSheetName, r);
rows.push(i + 1);
} else if (r[1].substring(0, 5) == "FEDEX") { //using susbstring (first 5 letters of value in col 2) so that we can match anything starting with...
moveToSheet(fedId, fedSheetName, r);
rows.push(i + 1);
} else {
writeBack.push(r);
}
});
s.clear();
s.getRange(1, 1, writeBack.length, writeBack[0].length)
.setValues(writeBack);
}
function moveRow(e) {
var s = e.source.getActiveSheet();
var r = e.range.rowStart;
if (s.getName() !== "Sheet1" || e.range.columnStart !== firstColToWatch || e.value == "") return;
var val = s.getRange(e.range.rowStart, 1, 1, s.getLastColumn())
.getValues()[0];
if (e.value.indexOf("DHL") !== -1 && val[0] == "Watches") {
moveToSheet(dhlId, dhlSheetName, val);
s.deleteRow(r);
} else if (e.value.indexOf("DHL") !== -1 && val[0] == "Shirts") {
moveToSheet(dhlId, dhlSheetName, val);
s.deleteRow(r);
} else if (e.value.indexOf("UPS") !== -1 && val[0] == "Shoes") {
moveToSheet(upsId, upsSheetName, val);
s.deleteRow(r);
} else if (e.value.indexOf("UPS") !== -1 && val[0] == "Pants") {
moveToSheet(upsId, upsSheetName, val);
s.deleteRow(r);
} else if (e.value.indexOf("FEDEX") !== -1) {
moveToSheet(fedId, fedSheetName, val);
s.deleteRow(r);
}
}
function moveToSheet(Id, sheetName, val) {
var ss = SpreadsheetApp.openById(Id)
.getSheetByName(sheetName)
.appendRow(val);
}

非常感谢任何帮助!