Macro for saving sheet as single image in Calc

Hi!

I have an ods file, which has only one sheet. I’d like to generate PNG file of the sheet. However, if I use “File → Export…”, I got PNG files which is splitted into page. What I need is a PNG file as single image, not the splitted one.

Then I came up with an idea. First, I select the necessary area, copy, and paste it to Writer, like “(Right click on page) → Paste spetial → More options → Bitmap”. Then I right-clicked the pasted bitmap object and did “Save” in the context menu. With this method I was succeeded in generating the single PNG image manually, so I tried to automate this procedure, using Macro.

I used “Record Macro”, and found that saving Bitmap object corresponds to “.uno:SaveGraphic”; however, whatever kind of args did I passed to uno:SaveGraphic, a save dialogue window appears.

Are there any way to dispatch “.uno:SaveGraphic” without creating save dialogue? Or, are there any other simple way to get single PNG image?

Thank you

I made another follow-up question. For further discussion, please use this.

I’m sorry for inconvinience.

Not inconvenient at all. It is often better to ask multiple smaller questions rather than one large question.

Your approach seems to be the only good way to do it. However, use Draw instead of Writer. The following code does not require user interaction or pop up any dialogs.

Sub SaveSheetAsBitmap
    'Copy from Calc.'
    oSpreadsheet = ThisComponent
    oSpreadsheetController = ThisComponent.getCurrentController()
    oSheet = oSpreadsheetController.getActiveSheet()
    'oRange = oSheet.getCellRangeByname("A1:G100")'
    'oSpreadsheetController.select(oRange)'
    oSpreadsheetController.select(oSheet)
    oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    oSpreadsheetFrame = oSpreadsheetController.getFrame()
    oDispatcher.executeDispatch(oSpreadsheetFrame, ".uno:Copy", "", 0, Array())

    'Paste bitmap into Draw.'
    oDoc = StarDesktop.loadComponentFromUrl(_
        "private:factory/sdraw", "_blank", 0, Array())
    oDocController = oDoc.getCurrentController()
    oDocFrame = oDocController().getFrame()
    Dim props(0) As New com.sun.star.beans.PropertyValue
    props(0).Name = "SelectedFormat"
    props(0).Value = 2  'Apparently this means bitmap format'
    oDispatcher.executeDispatch(oDocFrame, ".uno:ClipboardFormatItems", "", 0, props())
    
    'Export to PNG file.'
    Dim aExportProps(1) as new com.sun.star.beans.PropertyValue
    aExportProps(0).Name = "URL"
    aExportProps(0).Value = "file:///path/to/test.png"
    aExportProps(1).Name = "MimeType"
    aExportProps(1).Value = "image/png"
    oExporter = createUnoService("com.sun.star.drawing.GraphicExportFilter")
    oExporter.SetSourceDocument(oDocController.Selection)
    oExporter.Filter(aExportProps)
End Sub

Thank you for the answer! I confirmed that this macro does what I wanted to. However, if I run this macro from shell, like “soffice --headless --norestore --nofirststartwizard --invisible “macro:///Standard.Module1.SaveSheetAsBitmap()” test.ods”, the soffice command does not return.

I thought that I should close the documents, so I added “oDoc.close(True)” and “oSpreadsheet.close(True)” at the end of “SaveSheetAsBitmap”. But the situation does not change.

What else do I need?

Running headless is a different story. To ask about that, post a new follow-up question with a link to this one.