[Solved] Basic Macro to Increment a Chart's Data Range

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
drjlevi6
Posts: 3
Joined: Mon Jan 21, 2013 8:17 pm

[Solved] Basic Macro to Increment a Chart's Data Range

Post by drjlevi6 »

I've written a (LibreOffice) Basic macro to increment a sample spreadsheet's (only) chart's data range by 1:

Code: Select all

Sub AdjustChartRange
Dim Doc As Object
Dim Chart As Object
Dim EndRow As Integer

Doc = ThisComponent
Chart = Doc.Sheets(0).Charts(0)
EndRow = Chart.Ranges(0).EndRow
Chart.Ranges(0).EndRow = EndRow + 1
End Sub
Everything seems to work until the last line: at End Sub, Chart.Ranges(0).EndRow still hasn't been incremented and the chart's data range is unchanged. Any ideas?

TIA, Jonathan

(Using LibreOffice Version 3.6.4.3, Mac OS X 10.8.2)
Last edited by Hagar Delest on Tue Jan 22, 2013 3:01 pm, edited 1 time in total.
Reason: tagged [Solved].
LibreOffice 3.6.4.3 with MacOS X 10.8.2
FJCC
Moderator
Posts: 9647
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Help: Basic Macro to Increment a Chart's Data Range

Post by FJCC »

It seems you have to replace the entire array of cell ranges, not just increment values in place.

Code: Select all

Doc = ThisComponent
Chart = Doc.Sheets(0).Charts(0)
Ranges = Chart.Ranges
Ranges(0).EndRow = Ranges(0).EndRow + 1
Chart.Ranges = Ranges
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31365
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Help: Basic Macro to Increment a Chart's Data Range

Post by Villeroy »

In all spreadsheet programs, the insertion of new cells expands existing references.
My macro expands everything[*] by insertion of cells: http://forum.openoffice.org/en/forum/vi ... =21&t=2350
In addition, it copies down any formulas and selects the new empty cells for editing.

[*] chart ranges, database ranges, named ranges, list/combo box source ranges, formula references in cells, validations, conditional formattings and UNO objects.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
drjlevi6
Posts: 3
Joined: Mon Jan 21, 2013 8:17 pm

Re: [Solved] Help: Basic Macro to Increment a Chart's Data R

Post by drjlevi6 »

FJCC, thank you very much. Your fix worked.

Villeroy, thank you too, but your solution of importing your Python scripts seems a little more out-of-the-way, even though it's more general. However, I'll bookmark your reply, since importing already-written Python scripts sounds potentially useful, too.

Jonathan
LibreOffice 3.6.4.3 with MacOS X 10.8.2
Post Reply