googleworkspace / apps-script-samples

Apps Script samples for Google Workspace products.
https://developers.google.com/apps-script
Apache License 2.0
4.49k stars 1.83k forks source link

could not set the background color the filtered rows. #440

Closed Nirajan1-droid closed 6 months ago

Nirajan1-droid commented 7 months ago

tried other methods as well, but didn't worked out. the display function over the filtered screen also seems to be not working.

function applyFilterConditionsAndColor(sheet) { var filterRange = sheet.getDataRange(); var existingFilter = filterRange.getFilter(); if (existingFilter !== null) { existingFilter.remove(); }

// Apply filter condition to column N where the value is "ToCall_Sheet" var criteria = SpreadsheetApp.newFilterCriteria() .whenTextEqualTo("ToCall_Sheet") .build();

filterRange.createFilter().setColumnFilterCriteria(14, criteria); // Assuming "ToCall_Sheet" is in column N (index 14)

// Get the filtered rows (excluding header) var filteredRows = filterRange.getValues().slice(1);

// Set background color to yellow for the filtered rows for (var i = 0; i < filteredRows.length; i++) { sheet.getRange(i + 2, 1, 1, sheet.getLastColumn()).setBackground('yellow'); } }

but the most fraustating thing is that this below code works out of nowhere: the sheet is the main subsheet where the operation is carried out. and the filter view is not accessed by it. but it is literally coloring the filtered rows. someone tell me it is not the fault of the compiler optimization error thing. function applyColor(sheet){ var DELETE_VAL = "SEND TO WHATSAPP"; var COL_TO_SEARCH = 14;

// Get the range values var rangeValues = sheet.getDataRange().getValues(); var range = sheet.getDataRange(); // Filter out rows where column N is "DELETE" var filteredValues = rangeValues.filter(function(row) { return row[COL_TO_SEARCH - 1] !== DELETE_VAL; });

// Clear the content of the entire sheet range.setBackground("red");

}

Nirajan1-droid commented 6 months ago

anyone ?? where is the response to the issue?

Nirajan1-droid commented 6 months ago

i have found the solution.