-1

how to make this script works on different sheet with different trigger? simple is make same script run twice

original script works on many sheets and hiding many columns on the 2nd script i make it simple, and reverse function. with current condition, original script works fine, and 2nd script doesnt work

Script Source

 function onEdit() { onEdit1(); onEdit2(); } function onEdit1() // Original Script { var ss = SpreadsheetApp.getActiveSpreadsheet(); ["Calc", "Sheet1", "Sheet2", "Sheet3","Sheet4"].forEach(function (s) { var sheet = ss.getSheetByName(s); /// works on many sheets var st =sheet.getRange("B2").getValue(); //trigger cell if(st == false) //trigger word { sheet.hideColumn(sheet.getRange("W:Z")); sheet.hideColumn(sheet.getRange("AA:AK")); sheet.hideColumn(sheet.getRange("AL:AN")); sheet.hideColumn(sheet.getRange("AO:AO")); sheet.hideColumn(sheet.getRange("AP:AP")); } else { sheet.unhideColumn(sheet.getRange("W:Z")); sheet.unhideColumn(sheet.getRange("A:K")); sheet.unhideColumn(sheet.getRange("AL:AN")); sheet.unhideColumn(sheet.getRange("AO:AO")); sheet.unhideColumn(sheet.getRange("AP:AP")); } })} function onEdit2() /// 2nd script { var ss = SpreadsheetApp.getActiveSpreadsheet(); ["Duo","Sheet1","Sheet2"].forEach(function (s) { var sheet = ss.getSheetByName(s); /// bisa di banyak sheet var st =sheet.getRange("AC5").getValue(); //trigger cell if(st == True) //trigger word { sheet.unhideColumn(sheet.getRange("AU:BH")); // i Reverse hide unhide } else { sheet.hideColumn(sheet.getRange("AU:BH")); //here too } })} 
2
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand your goal from how to make this script works on different sheet with different trigger? simple is make same script run twice. Can I ask you about the detail of your goal? Commented Oct 13, 2021 at 5:26
  • different sheet need to hide/unhide diffferent columns based on each trigger Commented Oct 13, 2021 at 5:48

1 Answer 1

1

Issue:

In JavaScript (and therefore, in Apps Script), the boolean values are true and false.

You are using True instead, with capital T, so most probably it's throwing an error because it's undefined (you can take a look at the Executions tab in your script in order to check this).

Solution:

In your onEdit2 function, change this:

if(st == True) //trigger word 

To this:

if(st == true) //trigger word 

Reference:

Sign up to request clarification or add additional context in comments.

3 Comments

done it, on script 1 type false/False/FALSE it works but script 2 it doesnt responding [Link to try] (docs.google.com/spreadsheets/d/…)
@yoenoess The script stops before reaching onEdit2 because it is trying to access ranges in non-existent sheets, because if the sheet name doesn't exist var sheet = ss.getSheetByName(s); will return null, and null doesn't have a method getRange. Either implement a try...catch or don't add non-existent sheets to your list (in your test spreadsheet there are only Calc and Duo, and in your list there are Sheet1, Sheet2, Sheet3, Sheet4).
awsome it works like magic, thankyouuu

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.