I'm still very new to Javascript, but I've been learning a lot.
I'm creating an intake form on Google Sheets where the user can make selections, and then depending on the selection on some dropdown rows, the sheet unhides certain rows beneath where the user can fill in more information. The function is triggered "on change", so I'm assuming that every time the sheet senses a change has been made, it then executes the function to hide or unhide rows.
As you can see in the attached GIF, my script works pretty well, but it is terribly slow, with some response times approaching 3-4 seconds before the script unhides the appropriate rows.
The code that I have so far works like this:
function selectionUnHidesRows() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const values = sheet.getDataRange().getValues();
// Someone else gave me the following code to create variables from whatever values are in column A (the "header" column) and then grabs the value to the right of it in column B (the "input" column)
const conversionObj = {
"Client Marital Status": "clientMaritalStatus",
"Client's Spouse's Name": "clientSpouseName",
"How many children?": "numChildren",
}
const obj = values.reduce((o, [a, b]) => {
if (conversionObj[a]) {
o[conversionObj[a]] = b;
}
return o;
}, {});
const {clientMaritalStatus, clientSpouseName, numChildren} = obj;
// This looks through the sheet and finds the required text from Column A, here "Client's Spouse's Name"
const clientSpouseTextFinder = sheet.createTextFinder("Client's Spouse's Name").findNext();
// once it's found the text, it gets the row
const clientSpouseRow = clientSpouseTextFinder.getRow();
switch(clientMaritalStatus) {
case "married":
sheet.showRows(clientSpouseRow); // if the dropdown next to "Client Marital Status" says "married", then show the row below that asks for the spouse's name
break;
default:
sheet.hideRows(clientSpouseRow); // if the dropdown has anything other than "married", then hide the row below
break;
}
}
Are there potentially any faster ways of achieving the same thing? I tried to figure out how to append rows instead of hiding/unhiding rows, but it seemed more complicated and possibly would take longer to add rows and input values into the requisite cells.