経費報告書のチュートリアルを使用して、ほとんどのコードを生成しました。
主な変更点は、経費報告書の 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);
}