Hello everybody
This is a how-to-question about Macro Coding in OnlyOffice Spreadsheets.
Technical Background: OnlyOffice V. 9.4.0 on MacOS and Linux Mint.
Usage Background: I connected one universal Macro to different pictures located in different cells i an OnlyOffice Spreadsheet (way above 200 cells in total). When the universal Macro is started at any of its locations, it will start its work in relation of its actual cell location.
- E.g.: Pictures are set into cells A1, B1, C1, … F1. One universal Macro is assigned to each of these pictures/cells. The Macro will copy the neighbour cell in the same row. So if the Macro in cell B1 is activated, cell B2 will be copied. Or if the Macro in cell F1 is activated, cell F2 will be copied.
-
let sourceSheet = Api.GetActiveSheet(); let activeCell = sourceSheet.GetActiveCell(); let row = activeCell.GetRow(); // Values from current sheet let value1 = sourceSheet.GetRange("B" + row).GetValue();
My Macro code is based on the location of the “active cell” (see code above). As a result I need to click on the actual cell where the Macro is attached to, so that this very cell is becoming the “active cell”. Then I may click on the picture in this cell to start the Macro.
Problem: The Macro is designed to work in relation to the “active cell” (the cell which is clicked on). But Macros can be started/clicked-on without making it’s current cell the active cell.
- E.g.: Cell A1 is active. It is the cell where I clicked on the latest. When I start the Macro by pressing the picture in cell E1 this very cell E1 will not become the active cell. The active cell will stay cell A1. As a result the Macro will copy cell A2 instead of E2 because it is designed to do so by code (the code works with the “active cell”).
There is no problem as long as I make the intended cell of the Macro to the “active cell” first (simply by clicking on the cell) and then starting the Macro in this cell. But I am human and make mistakes. Often I forget to make the inteded cell the “active cell” and the results I am getting are not correct, of course.
Goal: I would like to activate a Macro so this Macro will start its task in relation to its current cell position without the need to click on the respective cell first (to make it the “active cell” manually).
Solutions:
- Making the intended cell the “active cell” manually before starting the Macro (as describet above). Not the safest solution: I might forget to do this.
- Coding Macros with defined cell references (the Macro in cell A1 will reference to it self by using the cell A1 as fixed position in the code; the Macro in cell B1 will be referenced to B1 as well etc.). This will require a specific Macro for every intended cell. Not very handy for larger spreadsheets.
- Change the Macro Code in some beneficial way?
- Any other solution I am not aware of…
Question: Does anybody see a solution for this topic?
Thanks a lot. Best regards