Excel Online Script runs correctly in Automate but does not run correctly after you add to workbook

Michael H 20 Reputation points
2026-07-07T20:07:26.5033333+00:00

I have created a script to hide tabs based on the values in a drop-down on a sheet in my workbook. When I run the script from the Automate tab > View Scripts, everything works as expected. When I use the 3 dots to Add in Workbook, the script does not run as expected - it says it runs, but it posts my completed information in the wrong cells, and it does not hide the expected tabs.

Here is my current script

function main(workbook: ExcelScript.Workbook) {
    const directions = workbook.getWorksheet("Directions");

    // Adjust these cells if needed
    const customerName =
        directions.getRange("B4").getText();

    const purchasedFee =
        directions.getRange("C4").getText().trim().toUpperCase();

    const purchasedFood =
        directions.getRange("C5").getText().trim().toUpperCase();

    // Fee Tabs
    const feeSheets = [
        "Discounts",
        "Fee Codes",
        "Payment Types"
    ];

    // Food Tabs
    const foodSheets = [
        "Denial Reason",
        "Item Type",
        "Price Category"
    ];

    // =====================
    // PROCESS FEE TABS
    // =====================

    feeSheets.forEach(sheetName => {

        let sheet = workbook.getWorksheet(sheetName);

        if (sheet) {

            if (purchasedFee === "NO") {
                sheet.setVisibility(
                    ExcelScript.SheetVisibility.hidden
                );
            }
            else {
                sheet.setVisibility(
                    ExcelScript.SheetVisibility.visible
                );
            }

        }

    });

    // =====================
    // PROCESS FOOD TABS
    // =====================

    foodSheets.forEach(sheetName => {

        let sheet = workbook.getWorksheet(sheetName);

        if (sheet) {

            if (purchasedFood === "NO") {
                sheet.setVisibility(
                    ExcelScript.SheetVisibility.hidden
                );
            }
            else {
                sheet.setVisibility(
                    ExcelScript.SheetVisibility.visible
                );
            }

        }

    });

    // =====================
    // UPDATE STATUS
    // =====================

    directions.getRange("D7").setValue(
        "Workbook Processed"
    );

    directions.getRange("D8").setValue(
        new Date().toLocaleString()
    );

}


I am not sure why it runs from one spot correctly but when I want to add a button to my workbook for ease of use it does not.

Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments

Answer accepted by question author
Hendrix-C 20,005 Reputation points Microsoft External Staff Moderator
2026-07-07T21:47:28.2833333+00:00

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.

Was this answer helpful?

2 people found this answer helpful.

0 additional answers

Sort by: Newest

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.