A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Hi Michael,
The script logic you shared uses fixed worksheet names and fixed cell addresses, so the selected button location should not change where D7/D8 are written or which tabs are hidden. Therefore, in this case, the most likely cause is not the code itself but I suspect it can be due to one of these:
- The workbook button is running an older/different saved version of the script: Office Scripts are stored separately and associated with the workbook. For example, if you edited the script after adding the button, or if there are duplicate scripts with similar names, the button may still be linked to a different script identity than the one you are testing from View Scripts.
- The script may be reading dropdown display text differently: Your code uses
getText(). If the dropdown cell is formatted, contains extra spaces, has a non-breaking space, or displays something different from the underlying value, the button run may appear successful, but the condition may not match the expected "NO" - The script may be running successfully but with silent logic mismatch: need to check through View logs / the Output tab in the Office Scripts task pane.
Before changing the code, I suggest you should rebuild the button connection first:
- Open the script from Automate > View Scripts and make sure the latest code is saved.
- In the script details, turn "Associate with workbook" off and confirm removal of existing buttons.
- Close and reopen the workbook.
- Open the script again, turn "Associate with workbook" back on, then select "Add button to worksheet" to add a fresh new button to worksheet.
For a safer revised version, you can try this version to see if it works:
function main(workbook: ExcelScript.Workbook) {
const directions = workbook.getWorksheet("Directions");
workbook.getApplication().calculate(ExcelScript.CalculationType.full);
const normalizeYesNo = (value: string | number | boolean): string => {
return String(value ?? "").replace(/\u00A0/g, " ").trim().toUpperCase();
};
const customerName = String(directions.getRange("B4").getValue() ?? "").trim();
const purchasedFee = normalizeYesNo(directions.getRange("C4").getValue());
const purchasedFood = normalizeYesNo(directions.getRange("C5").getValue());
//rest of your script...
You can try these steps and let me know if it works for you. If not, please feel free to reach out again in the comments on this post and I'm happy to assist you further.Thank you for your understanding and cooperation. I'm looking forward to your reply.
If the answer is helpful, please click "Yes" and kindly upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.