Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Google Spreadsheet Scripting scripting does not add menu and filter does not work as expected

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;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. addItem‘s second argument is string. You are not calling addToUi()

ui.createMenu('Custom Formatting').addItem('Format Report', 'formatReport').addToUi();

  1. You are not calling the remove function

if (filter){filter.remove();}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading