Office script in Excel instead of VBA

ausswe

Board Regular
Joined
Feb 19, 2013
Messages
50
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi, I'm trying to get my head around Office Scripts in Excel in order to use them instead of VBA in a workbook that is shared with a large number of colleagues who don't have Office 365 Desktop - they access the files in Teams/SharePoint Web instead (hence why VBA isn't an option).

I have created an Excel file and saved it in SharePoint and then tried to use Copilot to create an Office Script that opens the workbook as read-only (except for some users) and also closing the file without offering to save the changes.

I added the script to the workbook and saved it in the same folder in SharePoint, but it doesn't seem to work so I'm wondering what I'm doing wrong.

Anyone who might have some insight?

This is the script:
Rich (BB code):
function main(workbook: ExcelScript.Workbook) {
    // List of users who are exempt from the script
    const exemptUsers = ["user1@email.com", "user2@email.com"];

    // Get the current user's email
    const currentUser = workbook.getApplication().getUserEmail();

    // Check if the current user is exempt
    if (!exemptUsers.includes(currentUser)) {
        // Set the workbook to read-only mode
        workbook.getApplication().setWorkbookReadOnly(true);

        // Close the workbook without saving
        workbook.close(ExcelScript.CloseBehavior.discard);
    }
}
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
What is causing your code to execute?

I do not have access to a version of Excel that supports Office Script, but my understanding is that it does not support event handling. That is, there is no way to write code that will execute when the file is opened. I have a feeling that you are the code you have written to run when the file opens, but it will only run if you explicitly run function main.
 
Upvote 0
What is causing your code to execute?

I do not have access to a version of Excel that supports Office Script, but my understanding is that it does not support event handling. That is, there is no way to write code that will execute when the file is opened. I have a feeling that you are the code you have written to run when the file opens, but it will only run if you explicitly run function main.
I think you are correct! After some searching I think the only way to potentially achieve it would be to also involve Power Automate or similar.

Thanks for the input!
 
Upvote 0

Forum statistics

Threads
1,226,466
Messages
6,191,196
Members
453,646
Latest member
SteenP

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top