I have a drawing on the Sheet to which I have assigned the following script.
The problem is when the drawing is clicked and the script is run, the Filter View get created but is not visible in Data > Change view. Also its not shown above the sheet whee the Filter View name is shown along with the Range.
Filter View
In order to make it visible in Data > Change view I have to change from the active sheet to another sheet and come back to the previous sheet and than go Data > Change view and select the Filter view manually.
How can i apply this automatically when the drawing is clicked.
function createFilterView() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
const spreadsheetId = ss.getId();
// Find the named range that starts with "tbl_"
const namedRanges = ss.getNamedRanges();
let namedRange;
for (const range of namedRanges) {
if (range.getName().startsWith("tbl_")) {
namedRange = range.getRange();
break;
}
}
if (!namedRange) {
Logger.log("No named range starting with 'tbl_' found.");
return;
}
// Extract range details
const startRowIndex = namedRange.getRow() - 2;
const endRowIndex = namedRange.getLastRow();
const startColumnIndex = namedRange.getColumn() - 1;
const endColumnIndex = namedRange.getLastColumn();
// Get the active user's email
const userEmail = Session.getActiveUser().getEmail();
// Define the resource for the FilterView
const resource = {
requests: [
{
addFilterView: {
filter: {
range: {
sheetId: sheet.getSheetId(),
startRowIndex: startRowIndex,
endRowIndex: endRowIndex,
startColumnIndex: startColumnIndex,
endColumnIndex: endColumnIndex
},
title: userEmail // Set the filter view title to the user's email
}
}
}
]
};
// Apply the filter view to the active sheet
Sheets.Spreadsheets.batchUpdate(resource, spreadsheetId);
}
Procurement Mosaic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.