mercredi 30 mai 2018

Google Apps Script - Sheets - IF statement in a script

I have 300,000 cells that all use the same base IF statement (copying/pasting over and down updates the respective row or column reference). This is used to populate a Gantt chart, comparing the column header's date value to another cell in the same row. Depending on the values found, the outputs are a letter, or nothing at all.

The issue is, as you can imagine, performance - it's very very slow. Any time I do anything on the Sheet, it seems, every IF statement is re-evaluated, and the performance is very poor.

Is there a way to remove this IF statement in the cells, and add it as a function, only calling it via a menu when I'd like the Gantt chart to update?

Here's the IF statement:

=IF($K2="N/A","",IF(AND(L$1>=$H2,L$1<=$I2),IF(ISBLANK($E2),"A",IF(ISBLANK($F2),"B",IF($G2="Crew","D","C"))),""))

I've tried a few different ways, but I'm stumped on how a single inputted formula in GAS could be evaluated for every cell with their own respective column and row references?

// Adds a custom menu
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Refresh Data')
  .addItem('Update Gantt', 'setIfForGantt')
  .addToUi();
}

// Set IF statement for Gantt chart
function setIfForGantt() {
  var sheetName = "Project Management";
  var range = "L:CW";

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  var range = sheet.getRange(range);
  var values = range.getValues();
  var formula = ['IF($K2="N/A","",IF(AND(L$1>=$H2,L$1<=$I2),IF(ISBLANK($E2),"A",IF(ISBLANK($F2),"B",IF($G2="Crew","D","C"))),""))'];

Aucun commentaire:

Enregistrer un commentaire