Quick editing of XLSX files

Ever felt the need to modify the content of Excel XLSX files?

Well thanks to Keisuke opening up the intricate content of a Microsoft OPC file (docx;xlsx;pptx;etc…) we can now easily edit the content of the Excel spreadsheets fiddling around with XML.

Or rather, it could be easy if Microsoft had devised their open document formats in a straightforward way, but being Microsoft and having written the decisive chapters of the industry standard cookbook obfuscating code for sociopaths  they have made it as complicated as possible.

Well I can at least help alleviate the pain a bit with a little component you can download here:

Download AC_XlsxEdit from Dropbox

You will need to download the OPC Plugin from Keisukes Github repository:

Keisukes OPC Plugin on Github

The component is V14 but should work with all versions of 4D.

It does what it says, thus you can:

  • get a list of worksheets
  • get a cell value
  • set a cell value
  • save the modified worksheet
  • some useful other stuff, just see for yourself

Its not very sophisticated, you will have to clean up the XML after you are done with it using DOM CLOSE XML, failing to do so will make your application hemorrhage memory as if that stuff was going out of fashion.

There is a method called _documentation, containing, you guessed it, example code!
All methods are documented in code.

Also calling the error handling rudimentary might qualify for a medal at the annual Euphemism World Cup.

Also I obviously decline any and all responsibility for what you do with it, gift horse and all…

That’s it folks!


and for the impatient here is a quite silly code example that sets the value of row 17, column 3 in the worksheet called “Revenue” to some text and returns the old value:

If (False)
// ----------------------------------------------------
// Parameter Definition
// ----------------------------------------------------
// Local Variable Definition
  ARRAY TEXT($at_Sheets;0)
// ----------------------------------------------------
End if 
//select a document
$t_Doc:=Select document("";"xlsx";"Choose Your Spreadsheet";0)
If (OK=1)
//load the list of sheets
$l_Count:=XLSX_WorkSheetList ($t_Doc;->$at_Sheets)
$l_Sheet:=Find in array($at_Sheets;"Revenue")
  If ($l_Sheet<1)
    ALERT("No sheet named REVENUE found")
//load the sheet
$tx_SheetXmlRef:=XLSX_WorkSheetLoad ($t_Doc;$l_Sheet)
//get the dimensions (not needed if you know you worksheet)
$l_Cols:=XLSX_WorkSheetColums ($tx_SheetXmlRef)
$l_Rows:=XLSX_WorkSheetRows ($tx_SheetXmlRef)
    Case of 
    : ($l_Cols<3)
      ALERT("not enough columns")
    : ($l_Rows<17)
      ALERT("not enough rows")
//calculate the cell coordinates
$t_Cell:=XLSX_CellCode (3;17)
//now get the current value
XLSX_CellGetValue ($t_Doc;$tx_SheetXmlRef;$t_Cell;->$t_ValueOld)
//now set the new value
$t_Value:="My Test Value"
     XLSX_CellPutValue ($tx_SheetXmlRef;$t_Cell;->$t_Value)
//last: save the sheet back to the document
     XLSX_WorkSheetSave ($t_Doc;$l_Sheet;$tx_SheetXmlRef)
   End case 
 End if 
  DOM CLOSE XML($tx_SheetXmlRef)
End if 

1 thought on “Quick editing of XLSX files

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.