summaryrefslogtreecommitdiff
path: root/wizards/source/sfdocuments/SF_Calc.xba
diff options
context:
space:
mode:
Diffstat (limited to 'wizards/source/sfdocuments/SF_Calc.xba')
-rw-r--r--wizards/source/sfdocuments/SF_Calc.xba109
1 files changed, 109 insertions, 0 deletions
diff --git a/wizards/source/sfdocuments/SF_Calc.xba b/wizards/source/sfdocuments/SF_Calc.xba
index f7cd2005d1b5..66fc9c19800f 100644
--- a/wizards/source/sfdocuments/SF_Calc.xba
+++ b/wizards/source/sfdocuments/SF_Calc.xba
@@ -85,6 +85,7 @@ Private Const DUPLICATESHEETERROR = "DUPLICATESHEETERROR"
Private Const OFFSETADDRESSERROR = "OFFSETADDRESSERROR"
Private Const CALCFORMNOTFOUNDERROR = "CALCFORMNOTFOUNDERROR"
Private Const DUPLICATECHARTERROR = "DUPLICATECHARTERROR"
+Private Const RANGEEXPORTERROR = "RANGEEXPORTERROR"
REM ============================================================= PRIVATE MEMBERS
@@ -1155,6 +1156,113 @@ Finally:
End Function ' SF_Documents.SF_Calc.DSum
REM -----------------------------------------------------------------------------
+Public Function ExportRangeToFile(Optional ByVal Range As Variant _
+ , Optional ByVal FileName As Variant _
+ , Optional ByVal ImageType As Variant _
+ , Optional ByVal Overwrite As Variant _
+ ) As Boolean
+''' Store the given range as an image to the given file location
+''' Actual selections are not impacted
+''' Inspired by https://stackoverflow.com/questions/30509532/how-to-export-cell-range-to-pdf-file
+''' Args:
+''' Range: sheet name or cell range to be exported, as a string
+''' FileName: Identifies the file where to save. It must follow the SF_FileSystem.FileNaming notation
+''' ImageType: the name of the targeted media type
+''' Allowed values: jpeg, pdf (default) and png
+''' Overwrite: True if the destination file may be overwritten (default = False)
+''' Returns:
+''' False if the document could not be saved
+''' Exceptions:
+''' RANGEEXPORTERROR The destination has its readonly attribute set or overwriting rejected
+''' Examples:
+''' oDoc.ExportRangeToFile('SheetX.B2:J15", "C:\Me\Range2.png", ImageType := "png", Overwrite := True)
+
+Dim bSaved As Boolean ' return value
+Dim oSfa As Object ' com.sun.star.ucb.SimpleFileAccess
+Dim sFile As String ' Alias of FileName
+Dim vStoreArguments As Variant ' Array of com.sun.star.beans.PropertyValue
+Dim vFilterData As Variant ' Array of com.sun.star.beans.PropertyValue
+Dim FSO As Object ' SF_FileSystem
+Dim vImageTypes As Variant ' Array of permitted image types
+Dim vFilters As Variant ' Array of corresponding filters in the same order as vImageTypes
+Dim sFilter As String ' The filter to apply
+Dim oSelect As Object ' Currently selected range(s)
+Dim oAddress As Object ' Alias of Range
+
+Const cstImageTypes = "jpeg,pdf,png"
+Const cstFilters = "calc_jpg_Export,calc_pdf_Export,calc_png_Export"
+
+Const cstThisSub = "SFDocuments.Calc.ExportRangeToFile"
+Const cstSubArgs = "Range, FileName, [ImageType=""pdf""|""jpeg""|""png""], [Overwrite=False]"
+
+ If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo CatchError
+ bSaved = False
+
+Check:
+ If IsMissing(ImageType) Or IsEmpty(ImageType) Then ImageType = "pdf"
+ If IsMissing(Overwrite) Or IsEmpty(Overwrite) Then Overwrite = False
+
+ vImageTypes = Split(cstImageTypes, ",")
+ If ScriptForge.SF_Utils._EnterFunction(cstThisSub, cstSubArgs) Then
+ If Not _IsStillAlive() Then GoTo Finally
+ If Not ScriptForge.SF_Utils._Validate(Range, "Range", V_STRING) Then GoTo Finally
+ If Not ScriptForge.SF_Utils._ValidateFile(FileName, "FileName") Then GoTo Finally
+ If Not ScriptForge.SF_Utils._Validate(ImageType, "ImageType", V_STRING, vImageTypes) Then GoTo Finally
+ If Not ScriptForge.SF_Utils._Validate(Overwrite, "Overwrite", ScriptForge.V_BOOLEAN) Then GoTo Finally
+ End If
+
+ ' Check destination file overwriting
+ Set FSO = CreateScriptService("FileSystem")
+ sFile = FSO._ConvertToUrl(FileName)
+ If FSO.FileExists(FileName) Then
+ If Overwrite = False Then GoTo CatchError
+ Set oSfa = ScriptForge.SF_Utils._GetUNOService("FileAccess")
+ If oSfa.isReadonly(sFile) Then GoTo CatchError
+ End If
+
+Try:
+ ' Setup arguments
+ vFilters = Split(cstFilters, ",")
+ sFilter = vFilters(ScriptForge.SF_Array.IndexOf(vImageTypes, ImageType, CaseSensitive := False))
+ Set oAddress = _ParseAddress(Range)
+
+ ' The filter arguments differ between
+ ' 1) pdf : store range in Selection property value
+ ' 2) png, jpeg : save current selection, select range, restore initial selection
+ If LCase(ImageType) = "pdf" Then
+ vFilterData = Array(ScriptForge.SF_Utils._MakePropertyValue("Selection", oAddress.XCellRange) )
+ vStoreArguments = Array( _
+ ScriptForge.SF_Utils._MakePropertyValue("FilterName", sFilter) _
+ , ScriptForge.SF_Utils._MakePropertyValue("FilterData", vFilterData) _
+ )
+ Else ' png, jpeg
+ ' Save the current selection(s)
+ Set oSelect = _Component.CurrentController.getSelection()
+ _Component.CurrentController.select(oAddress.XCellRange)
+ vStoreArguments = Array( _
+ ScriptForge.SF_Utils._MakePropertyValue("FilterName", sFilter) _
+ , ScriptForge.SF_Utils._MakePropertyValue("SelectionOnly", True) _
+ )
+ End If
+
+ ' Apply the filter and export
+ _Component.storeToUrl(sFile, vStoreArguments)
+ If LCase(ImageType) <> "pdf" Then _RestoreSelections(_Component, oSelect)
+
+ bSaved = True
+
+Finally:
+ ExportRangeToFile = bSaved
+ ScriptForge.SF_Utils._ExitFunction(cstThisSub)
+ Exit Function
+Catch:
+ GoTo Finally
+CatchError:
+ ScriptForge.SF_Exception.RaiseFatal(RANGEEXPORTERROR, "FileName", FileName, "Overwrite", Overwrite)
+ GoTo Finally
+End Function ' SFDocuments.SF_Chart.ExportRangeToFile
+
+REM -----------------------------------------------------------------------------
Public Function Forms(Optional ByVal SheetName As Variant _
, Optional ByVal Form As Variant _
) As Variant
@@ -1650,6 +1758,7 @@ Public Function Methods() As Variant
, "DMax" _
, "DMin" _
, "DSum" _
+ , "ExportRangeToFile" _
, "GetColumnName" _
, "GetFormula" _
, "GetValue" _