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:
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);
}
}