Office Scripts: setting value for selected cells

dougmarkham

Active Member
Joined
Jul 19, 2016
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I have been asked to generate an online excel spreadsheet and automate some functions that I would normally use VBA for.
I've read a bit of intro material, but Office Script seems alien to my brain right now.

How would one replicate the following VBA code?

VBA Code:
Sub setvalue()

Dim ws As Worksheet: Set ws = Worksheets("Sheet1")

ws.Activate

Selection.Value = "Yes"

End Sub

I'm trying to understand how the syntax works, but this doesn't seem to be covered in the introductory stuff, so any help would be greatly appreciated!

Kind regards,

Doug.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I think the equivalent code would look something like this:
function main(workbook: ExcelScript.Workbook) { let ws = workbook.getWorksheet('Sheet1'); ws.activate() workbook.getSelectedRange().setValue("Yes") }
 
Upvote 0
Hi *b_gonzalez,

Thanks for replying.
I've tried your script and it ran with an error: Line 4: Workbook getSelectedRange: The current selection is invalid for this operation.

OfficeScriptGetSelectedRangeError.PNG


Not deterred, I tried suing getSelectedRanges and setValues

OfficeScriptGetSelectedRangeError2.PNG


I got the error: Line 5: selectedRanges.setValues is not a function.

Not sure why it's saying the function isn't valid.

Any ideas?

Kind regards,

Doug.
 
Upvote 0
For discontinuous ranges like this, you have to use getSelectedRanges(). Get selected ranges returns a rangeAreas object. You access that by using the getAreas() method which returns a range array. From there, you can iterate through the array and use the setValue method like in the earlier example. The updated code would look something like this:

JavaScript:
function main(workbook: ExcelScript.Workbook) {
  let ws = workbook.getWorksheet('Sheet1');
  ws.activate()
  workbook.getSelectedRanges().getAreas().forEach(e=>e.setValue("Yes"))
}
 
Upvote 0
Hi *b_gonzalez,

Awesome! That works well :) Thanks for your help.

Just wondering if you know of any books or resources to help learn Office Scripts, as it's quite different from VBA, but looks to be the future?

Kind regards,

Doug.
 
Upvote 0
Sure you're welcome.

In terms of resources here are a few places you can look into:

Microsoft Learn for Office Scripts: Office Scripts in Excel - Office Scripts

Office Scripts page on StackOverflow. The questions are followed / answered by some of the Office Scripts dev team at Microsoft. I participate a lot on there as well: Newest 'office-scripts' Questions

Outside of those, good resources for learning Office Scripts don't really exist yet. But that will probably change some time later this year as more people start picking it up.
 
Upvote 0
Sure you're welcome.

In terms of resources here are a few places you can look into:

Microsoft Learn for Office Scripts: Office Scripts in Excel - Office Scripts

Office Scripts page on StackOverflow. The questions are followed / answered by some of the Office Scripts dev team at Microsoft. I participate a lot on there as well: Newest 'office-scripts' Questions

Outside of those, good resources for learning Office Scripts don't really exist yet. But that will probably change some time later this year as more people start picking it up.
Hi b_gonzalez,

Thanks so much for that!
I've got an interest in this now: forgotten how it felt not to know anything about VBA and how much fun the journey was of slowly picking up how it works.

I used a small office script to insert 'today's date' in the active cell and assigned the script to a button. I was curious at how different the syntax looks. Pipped my interest.

Was surprised about one thing though: when pressing the button, a run-time popup message comes up and there was a delay of several seconds before the script started. Running the same script from the edit window was much quicker. I wonder if there is a way to quell the popup?

I think I'll make some small projects to get going and have some time each day to get my head around the language---so thanks again for the info sources 🙂 🍻
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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