JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
1,013 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi everyone,
I hope you are well, I am new with Office Scripts, any advice is welcome, appreciated and most of all thanked.
I have a code (VBA) that is working correctly in an Excel Desktop (365), but in the online version that I share with my colleagues, it just doesn't work.
I am trying to adjust/convert this VBA code to an Office Script one, but I can't get it to work, it doesn't give me any error but still does what I expect.
I share with you both codes hoping you can help me with this mess. First, the VBA that works correctly and then my Script attempt.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim CeldaActiva As Range
Set CeldaActiva = Target.Cells(1)
If Not Application.Intersect(CeldaActiva, Range("A5:G10")) Is Nothing Then
Sheet1.Range("K1").Value = CeldaActiva.Value
End If
End Sub
OfficeScript.gif
function main(workbook: ExcelScript.Workbook) {
// function Worksheet_SelectionChange() { let CeldaActiva = context.workbook.getActiveCell(); CeldaActiva.load("value"); return context.sync() .then(function () { let rangeA5G10 = context.workbook.worksheets.getItem("Sheet1").getRange("A5:G10"); let intersectRange = rangeA5G10.getIntersection(CeldaActiva); if (intersectRange != null) { let k1Range = context.workbook.worksheets.getItem("Sheet1").getRange("K1"); k1Range.values = CeldaActiva.value; } }) .catch(function (error) { console.log(error); }); }
}