Skip to content

Instantly share code, notes, and snippets.

@mitsugu
Last active November 28, 2023 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitsugu/35117bba219fa2301a94b353526e9887 to your computer and use it in GitHub Desktop.
Save mitsugu/35117bba219fa2301a94b353526e9887 to your computer and use it in GitHub Desktop.
Code to retrieve the bottom row data of the final sheet of a Libreoffice Calc document specified by URL and column number.

What's this

User-defined functions for Libreoffice

What can you do

  • You can get the row number of the cell containing the data in the Libreoffice Calc sheet.
  • You can get the column number of the cell containing the data in the Libreoffice Calc sheet.
  • You can get the data in the last row of cells containing data in a Libreoffice Calc sheet.

Usage

  1. Import getEndCell.bas.
  2. Use functions in getEndCell.bas.

License

MIT License

function getLastRow(url as string)
Dim oArgs(0) As New com.sun.star.beans.PropertyValue
Dim oSheet As Object
oArgs(0).Name="Hidden"
oArgs(0).Value= true
if 0 < LEN(url) then
oDoc = StarDesktop.LoadComponentFromUrl(url, "_blank", 0, oArgs())
oSheet = oDoc.getSheets().getByIndex(oDoc.Sheets.Count-1)
else
oSheet = ThisComponent.CurrentController.Activesheet
end if
oRange = oSheet.getCellRangeByName("A1")
oCursor = oSheet.createCursorByRange(oRange)
oCursor.gotoEndOfUsedArea(True)
getLastRow = oCursor.Rows.Count
if 0 < LEN(url) then
oDoc.close(true)
end if
end function
function getLastColumn(url as string)
Dim oArgs(0) As New com.sun.star.beans.PropertyValue
Dim oSheet As Object
oArgs(0).Name="Hidden"
oArgs(0).Value= true
if 0 < LEN(url) then
oDoc = StarDesktop.LoadComponentFromUrl(url, "_blank", 0, oArgs())
oSheet = oDoc.getSheets().getByIndex(oDoc.Sheets.Count-1)
else
oSheet = ThisComponent.CurrentController.Activesheet
end if
oRange = oSheet.getCellRangeByName("A1")
oCursor = oSheet.createCursorByRange(oRange)
oCursor.gotoEndOfUsedArea(True)
getLastColumn = oCursor.Columns.Count
if 0 < LEN(url) then
oDoc.close(true)
end if
end function
function getBottomCellValue(url as string, num as integer)
Dim oArgs(0) As New com.sun.star.beans.PropertyValue
oArgs(0).Name="Hidden"
oArgs(0).Value= true
oDoc = StarDesktop.LoadComponentFromUrl(url, "_blank", 0, oArgs())
oSheet = oDoc.getSheets().getByIndex(oDoc.Sheets.Count-1)
oRange = oSheet.getCellRangeByName("A1")
oCursor = oSheet.createCursorByRange(oRange)
oCursor.gotoEndOfUsedArea(True)
row = oCursor.Rows.Count
oCell = oSheet.getCellByPosition(num-1,row-1)
getBottomCellValue = oCell.value
oDoc.close(true)
end function
MIT License
Copyright (c) 2023 mitsugu oyama
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment