Home > Software design >  Google Spreadsheet Scripting scripting does not add menu and filter does not work as expected
Google Spreadsheet Scripting scripting does not add menu and filter does not work as expected

Time:12-21

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:

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

  • Related