Reputation: 23
I am trying to all the data from a range in a spreadsheet and filter it based on a certain condition where item[27] and item [29] equals "CO". It's working fine and filtering the range properly however I am trying to output only one item in the array which is item[3] and it's not doing that. It's giving me the filtered version of the entire range which is good but I don't want that.
This is basically a spreadsheet containing students and information about the work modules they have completed. I want to filter only the names of the students which is item[3] who have a "CO" or "complete" marked against a certain work module into another sheet. I tried to map the filtered data in another array using data.map and then tried to output just one element of the array and that is not working as well.
BTW this is my first time coding anything and using Google Apps script. I am really interested in this stuff and hopefully, one day be half as good as anyone here. Any help will me much appreciated.
function ReviewReport() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mastersheet = ss.getSheetByName("Master Sheet 18-19-20");
var newtab = ss.getSheetByName("ReviewReportTest(Amith)");
var totalcolumns = mastersheet.getLastColumn();
var totalrows = mastersheet.getLastRow();
var masterdata = mastersheet
.getRange(4, 1, totalrows - 3, totalcolumns)
.getDisplayValues();
var data = masterdata.filter(function (item) {
// return item[27] === "CO" && item [29] === "CO";
if (item[27] === "CO" && item[29] === "CO") {
return item[3];
}
});
//tried to map the filtered data into another array below but this is not working as well.
var bsb = data.map(row);
function row(item) {
return item[3];
}
newtab.clearContents();
newtab.getRange(1, 1, data.length, data[0].length).setValues(data);
}
Upvotes: 2
Views: 89
Reputation: 38131
To write a single value from an Array object use Class Range setValue(value)
where value could be data[rowIndex][columnIdx]
.
bsb
was declared but not used later.Array.prototype.map
and it's callback are declared inside the same scope but map is used before the callback declaration. I think that it's better to declare it before calling it, or declare the callback global scope, but this is completely up to you. Think about style, readability and maintainability.Resource
Related
Upvotes: 1