I have a master sheet and a filtered sheet. The filtered sheet shows exactly the same number of columns as the master sheet, but the rows are filtered. I want to run the below script from the filtered sheet, so that it then updates the filtered data onto the master sheet with "INVOICED" in columnA.
I can copy the filtered data onto the master sheet, but it creates duplicate rows, which is not what I want. The below code doesn't do that, as I have progressed a little further. In fact, the below code doesn't seem to do anything lol.
Snippet of my code is as follows:
function courier_sales() {
var ss = SpreadsheetApp.getActive();
var srcSheet2 = ss.getSheetByName("COURIER_TEMPBIN2");
var dstSheet2 = ss.getSheetByName('COURIER HISTORICAL');
//***srcSheet2/dstSheet2 */
var data_range2 = srcSheet2.getRange('A2:W');
var data_data2 = data_range2.getValues();
var data_clean2 = data_data2.filter(function (r) {return r[0]});
var lr2 = dstSheet2.getLastRow();
if (data_clean2.length == 0) return;
dstSheet2.getRange(lr2+1, 1,data_clean2.length,23).setValues(data_clean2);
const rangeList = data_data2.reduce((ar, [a, ...b], i) => {
if (!a && b.join("")) {
ar.push(`A${i + 2}`);
}
return ar;
}, []);
srcSheet2.getRangeList(rangeList).setValues('INVOICED');
}
updates the filtered data onto the master sheet with "INVOICED" in columnA.
It appears that you're simply trying to update the status of every master sheet
row that matches the job number given in criteria!A1
. There is no need to employ a third sheet just to get those rows. Instead, use Array.map()
, like this:
function setInvoicedByCriteria() {
const ss = SpreadsheetApp.getActive();
const jobNumber = ss.getRange('criteria!A1').getValue();
const statusRange = ss.getRange('master sheet!A2:A');
const jobsNumbers = ss.getRange('master sheet!E2:E').getValues().flat();
const newStatuses = statusRange.getValues().flat()
.map((status, i) => jobsNumbers[i] === jobNumber ? ['INVOICED'] : [status]);
statusRange.setValues(newStatuses);
}
See Array.map().