Note that there are some explanatory texts on larger screens.

plurals
  1. POMatch Value in Two Spreadsheets, If Same Copy Cells from 1 and Paste to 2nd
    primarykey
    data
    text
    <p>I used the <a href="https://developers.google.com/apps-script/articles/expense_report_approval" rel="nofollow">expense report tutorial</a> to generate most of my code. </p> <p>The main thing I changed is that instead of the expense report ID being the row number, I made it into a random number (ll-nnnn). I would like to use the report ID as the identifier between the expense report sheet and the expense approvals sheet, so I added a report ID column to the report sheet. I want the script to take the report ID from the approvals form, and search the report sheet ID column for the row with that ID in it, then enter in whether it was approved or denied and whatever comments the manager had in the corresponding row in column M and N. In VBA the the code I would use would be and If statement with a match condition. I am new to java script so I am not sure how to do this. ***I have edited my code using what fooby provided. When I run it, I get the error "The coordinates or dimensions of the range are invalid." on the line: " poSheet.getRange(rowToUpdate, 2, 1, 13).setValues(updateInfo);" Any suggestions would be greatly appreciated! </p> <pre><code> 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload