我使用费用报告教程来生成我的大部分代码。
我更改的主要内容是费用报告 ID 不是行号,而是我将其设置为随机数 (ll-nnnn)。我想使用报表 ID 作为费用报表表和费用审批表之间的标识符,所以我在报表表中添加了一个报表 ID 列。我希望脚本从批准表单中获取报告 ID,并在报告表 ID 列中搜索具有该 ID 的行,然后输入它是被批准还是被拒绝,以及经理在相应行中的任何评论M 和 N 列。在 VBA 中,我将使用的代码是带有匹配条件的 if 语句。我是 java 脚本的新手,所以我不知道该怎么做。***我已经使用 fooby 提供的内容编辑了我的代码。当我运行它时,我收到错误“范围的坐标或尺寸无效”。上线:“ poSheet.getRange(rowToUpdate, 2, 1, 13).setValues(updateInfo);” 任何建议将不胜感激!
var PO_SPREADSHEET_ID= "0At0Io3p64FeddFVCV2lJdEpLY09MYWNkVS05NDhzWkE";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var poSs = SpreadsheetApp.openById(PO_SPREADSHEET_ID);
var poSheet = poSs.getSheets()[0];
function updatePOSheet() {
// Get IDs from Approval Sheet
var row = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
// Get IDs from Report Sheet, flatten it into a 1D array
var poId = poSheet.getRange(2,1,poSheet.getLastRow()-1,1).getValues().reduce(flatten_);
// For each row in the Approval Sheet update the coresponding report row
// reportIds is passed a "this" in the forEach function, see below
row.forEach(updateReportRow_,poId);
}
// Checks to see if the status of a row is either "Approved" or "Denied
// then updated the correct row in the reports sheet with manager's comments
function updateReportRow_(row) {
var id = row[2];
var status = row[3];
var comments = row[4];
// Get row in reports sheet from reportIds (it was passed as 'this')
var rowToUpdate = this.indexOf(id) + 1;
// Put info into spreadsheet friendly array
var updateInfo = [[status,comments]];
if (status == "Approved" || status == "Denied") {
poSheet.getRange(rowToUpdate, 2, 1, 13).setValues(updateInfo);
}
}
// Returns a piece of an array concatenated with the element on to it's right
// will make [[0],[1],[2]...]] into [0,1,2,...]
function flatten_(a,b) {
return a.concat(b);
}