Home > Blockchain >  Google apps script loop to toggle font size and color a cell value
Google apps script loop to toggle font size and color a cell value

Time:11-10

I am trying to toggle a cell value in 8 sheets of a spreadsheet basically to highlight the cell value and get the reader's attention

I have an onEdit function with other functions,

function onEdit(){
  cp = SpreadsheetApp.getActiveSpreadsheet();

  // The fucntion for hide/unhide sheets/tabs
  var sheet1 = cp.getSheetByName("1 s");
  var sheet2 = cp.getSheetByName("2 s");
  var sheet3 = cp.getSheetByName("3 s");
  var sheet4 = cp.getSheetByName("4 s");
  var sheet5 = cp.getSheetByName("5 s");
  var sheet6 = cp.getSheetByName("6 s");
  var sheet7 = cp.getSheetByName("7 s");
  var sheet8 = cp.getSheetByName("8 s");
  var Overview = cp.getSheetByName("Overview");

  var [cella, cellb, cellc, celld, celle, cellf, cellg, cellh, celli] = Overview.getRange("C6:C14").getValues().flat();
  if (cella =="Yes") { sheet1.showSheet(); } else { sheet1.hideSheet(); }
  if (cellb == "Yes") { sheet2.showSheet(); } else { sheet2.hideSheet(); }
  if (cellc == "Yes") { sheet3.showSheet(); } else { sheet3.hideSheet(); }
  if (celld == "Yes") { sheet4.showSheet(); } else { sheet4.hideSheet(); }
  if (celle == "Yes") { sheet5.showSheet(); } else { sheet5.hideSheet(); }
  if (cellf == "Yes") { sheet6.showSheet(); } else { sheet6.hideSheet(); }
  if (cellg == "Yes") { sheet7.showSheet(); } else { sheet7.hideSheet(); }
  if (celli == "Yes") { sheet8.showSheet(); } else { sheet8.hideSheet(); }


  // The fucntion for hide/unhide row in 8 s sheet
  var eos = cp.getSheetByName("8 s");
  var cellx = eos.getRange("D13").getValue();
  if ((cellx == "E") || ((cellx == ""))) { eos.hideRows(15); } else { eos.showRows(15); }



  // The fucntion for hide/unhide rows in 1 s 
  var general_info = cp.getSheetByName("0 S");
  var check = general_info.getRange("J9").getValue();
  if (check == "Dir") { sheet1.hideRows(37, 8); } else {
    if ((check == "Par") || (check == "")) {
      sheet1.showRows(37, 8);
    }
  }

// script for toggle
  var toggle = sheet8.getRange("G2");

  for(var i=0;i<50;i  ) {
    if( i%2 == 0 ){
      toggle.setFontColor("red");
      toggle.setFontSize(12);
    }
   else{
      toggle.setFontColor("black");
      toggle.setFontSize(10);
    }
  }
  SpreadsheetApp.flush();
  Utilities.sleep(100);


}

The script for toggle code block uses a for loop and changes the font color and size to grab attention.

Only the else part of the code block works upon edit and then it stops.

Is it because there are other code blocks of hiding/unhiding sheets and hiding/unhiding rows the toggle does not work.

I am not understanding why the cell does not toggle. Please help

CodePudding user response:

From your following reply,

its cell G8. The goal is to toggle the font size and color of the text value which is "Please fill all details in this sheet" in cell G8 in sheet8, This increase and decrease along with change of color will grab the attention of the onlooker

Is your expected result as follows? Please modify your script as follows.

From:

var toggle = sheet8.getRange("G2");

for(var i=0;i<50;i  ) {
  if( i%2 == 0 ){
    toggle.setFontColor("red");
    toggle.setFontSize(12);
  }
 else{
    toggle.setFontColor("black");
    toggle.setFontSize(10);
  }
}
SpreadsheetApp.flush();
Utilities.sleep(100);

To:

var toggle = sheet8.getRange("G8").activate(); // Modified
for (var i = 0; i < 50; i  ) {
  if (i % 2 == 0) {
    toggle.setFontColor("red");
    toggle.setFontSize(12);
  } else {
    toggle.setFontColor("black");
    toggle.setFontSize(10);
  }
  SpreadsheetApp.flush(); // Modified
  Utilities.sleep(100); // Modified
}
  • Related