I have the following code, I just write it for practising Google spreadsheet scripting
. I have two issues with my code. By the way, it runs and doesn't throw any errors. However, the issues are a) when I reload/open the spreadsheet it doesn't load the 'Custom Formatting' in the Google Sheet Menu. b) I was expecting that this will check if the filter is ON and if it's then remove the filter else add the filter. But it adds the filter everytime I run it. This is my very first code in Google Scripting. Please help me find my mistake.
function formatReport() {
let sheet= SpreadsheetApp.getActiveSpreadsheet();
let headrs = sheet.getRange('A1:F1');
let table = sheet.getDataRange();
let filter = sheet.getRange('A1:F1').getFilter();
if (filter){
filter.remove;
}
else{
headrs.setFontColor('white');
headrs.setBackground('#52489C');
table.setFontFamily('Roboto');
table.setHorizontalAlignment('center');
table.setBorder(true,true,true,true,false,true, '#52489C',SpreadsheetApp.BorderStyle.SOLID);
table.createFilter();
}
}
function onOpen(){
let ui =SpreadsheetApp.getUi();
ui.createMenu('Custom Formatting').addItem('Format Report', formatReport).addToUi;
}
CodePudding user response:
addItem
's second argument isstring
. You are not callingaddToUi()
ui.createMenu('Custom Formatting').addItem('Format Report', 'formatReport').addToUi();
- You are not calling the remove function
if (filter){filter.remove();}