1. ACF-Plugin Functions for Excel Spreadsheets

  1. ACF-Plugin Functions for Excel Spreadsheets
    1. Introduction
  2. Commands and Functions
    1. Functions for Creating and Managing Excel Files
      1. Excel_Create
      2. Excel_Open
      3. Excel_SetMetaData
      4. Excel_Close
      5. Excel_SaveAsWorkbook
    2. Functions for Working with Worksheets
      1. Excel_Add_Worksheet
      2. Exel_GetSheetID
      3. Excel_GetSheetName
      4. Excel_SetColumnWidth
    3. Functions for Populating Cells
      1. Excel_setCell
      2. Excel_setCellFormula
      3. Excel_SetColumns
      4. Excel_SetRows
      5. Excel_MergeCells
      6. Excel_SetConditionalFormatting
    4. Functions to retrive content from an Excel Workbook
      1. Excel_GetCell
      2. Excel_GetRowCount
      3. Excel_GetColCount
      4. Excel_GetRows
      5. Excel_GetColumns
      6. Excel_CountSheets
    5. Functions for Defining and Manipulating Styles
      1. Excel_AddStyle
      2. Excel_SetStyleOptions
      3. Excel_GetStyleOptions
      4. Excel_ListStyles
      5. Excel_CleanUnusedStyles
    6. Functions for Manipulating Rows and Columns
      1. Excel_InsertRows
      2. Excel_InsertColumns
      3. Excel_DeleteRows
      4. Excel_DeleteColumns
      5. Excel_ShiftRight
      6. Excel_ShiftLeft
      7. Complete example functions to manipulate rows
    7. Helper Functions
      1. Excel_SheetAddress
    8. Excel_ID and Sheet_ID

1.1. Introduction

The ACF-Plugin introduces a robust set of functions and commands to work with Excel spreadsheets in the XLSX format. This functionality lets you create, manipulate, and style spreadsheets directly within your ACF workflows. There are two primary use cases:

  1. Creating New Excel Spreadsheets: Fully generate new XLSX files with custom content and styles.
  2. Opening and Altering Existing Spreadsheets Extend functionality to open existing spreadsheets for editing.

2. Commands and Functions

2.1. Functions for Creating and Managing Excel Files

2.1.1. Excel_Create

Example (With some checking of overwrite an existing file):

Function TestExcel (string path)
    string res; 
    if ( path == "") then
        return "No path"; 
    end if
    if ( file_exists ( path)) then
        if ( confirm ("File exists, do you really want to overwrite it?")) then
            res = delete_file (path); 
        else
            return "File Exists";
        end if
    end if
    int wb = Excel_create ( path); 

2.1.2. Excel_Open

Example:

Function TestOpenExcel (string path)
    string res; 
    if ( path == "") then
        return "No path"; 
    end if
    if ( ! file_exists ( path)) then
        throw "No such file: " + path
    end if
    int wb = Excel_Open ( path, "r"); 

2.1.3. Excel_SetMetaData

Example:

Excel_SetMetaData (wb, JSON ("Author", "Me(Ole)","Created", now(), "Changed", now())); 

2.1.4. Excel_Close

Example:

    Excel_Close(wb); 
    return "OK"; 
end

2.1.5. Excel_SaveAsWorkbook

Example:

    Excel_SaveAsWorkbook(wb, "~/Desktop/MyNewExcelFile.xlsx");
  Excel_Close (wb); 
    return "OK"; 
end

2.2. Functions for Working with Worksheets

2.2.1. Excel_Add_Worksheet

Example:

int s2 = Excel_Add_Worksheet(wb, "Second sheet");

2.2.2. Exel_GetSheetID

Example:

int s1 = EXCEL_GetSheetID(wb, 1);

2.2.3. Excel_GetSheetName

Example: acf string SheetName = Excel_GetSheetName (sheet);

2.2.4. Excel_SetColumnWidth

Example:

    array float cw = {10,30,40,10,15.5};
    Excel_SetColumnWidth (s1, cw); 

2.3. Functions for Populating Cells

2.3.1. Excel_setCell

Example:

Excel_setCell ( s, 1, 1, "Excel sheet header", "H1");

Note: Setting a cell with an empty string creates an empty cell, still having the style applied. This is useful for colored areas with no content, in this example, we have a long text in the first column that is styled with a grey background. The content float over to the next cells. To have the cells in column 2-5 styled with the same color, we use this technique.

    Excel_setCell ( s, 2, 1, "Excel sheet subheader ", "Ctitles"); 
    Excel_setCell ( s, 2, 2, "", "Ctitles"); 
    Excel_setCell ( s, 2, 3, "", "Ctitles"); 
    Excel_setCell ( s, 2, 4, "", "Ctitles"); 
    Excel_setCell ( s, 2, 5, "", "Ctitles"); 

2.3.2. Excel_setCellFormula

Example:

    currentRow++;i++; 
    Excel_setCell ( s1, currentRow, 1, "SUM for this order", "SumLine");
    Excel_setCell ( s1, currentRow, 2, "", "SumLine"); 
    Excel_setCell ( s1, currentRow, 3, "", "SumLine"); 
    Excel_setCell ( s1, currentRow, 4, "", "SumLine"); 
    string formula = format("SUM(%s)", Excel_SheetAddress (3,5,currentRow-1,5)); 
    Excel_setCellFormula ( s1, currentRow, 5, formula, "SumLine"); 

2.3.3. Excel_SetColumns

Example:

array string values = {"Value1", "Value2", "Value3"};
Excel_SetColumns(1, 2, 1, values, "BoldStyle");

2.3.4. Excel_SetRows

2.3.5. Excel_MergeCells

2.3.6. Excel_SetConditionalFormatting

2.4. Functions to retrive content from an Excel Workbook

For importing existing Excel spreadsheets with Excel_Open the following functions can be used to retrive content and style information.

2.4.1. Excel_GetCell

2.4.2. Excel_GetRowCount

2.4.3. Excel_GetColCount

2.4.4. Excel_GetRows

2.4.5. Excel_GetColumns

2.4.6. Excel_CountSheets

2.5. Functions for Defining and Manipulating Styles

The function use a JSON object to define the style parameters. This is documentet on a separate page

2.5.1. Excel_AddStyle

Example:

    Excel_AddStyle ( wb, "H1"); // Cloned from Default
    Excel_SetStyleOptions (wb, "H1", JSON ("Font", "Areal", "FontSize", 20, "Decoration", "Bold, Italic"));  
    Excel_AddStyle ( wb, "Ctitles", "H1"); // Cloned from H1
    Excel_SetStyleOptions (wb, "Ctitles", 
        JSON ("FontSize", 16, "Decoration", "Bold", "FillColor", "DDDDDD", 
            "Borders",JSON("Bottom", linetype_thin)));
    Excel_AddStyle(wb, "data", JSON ("Font", "Century Gotic", "FontSize", 12, "Alignment", "Top"))
    Excel_AddStyle(wb, "wrap", "data"); 
    Excel_SetStyleOptions ( wb, "wrap", JSON ("TextWrap", true)); 
    Excel_AddStyle (wb, "num", "data"); 
    Excel_SetStyleOptions ( wb, "num", JSON ("NumFormat", "### ##0.00")); 
    Excel_AddStyle (wb, "SumLine", "num"); 
    Excel_SetStyleOptions ( wb, "SumLine", 
        JSON ("Decoration", "Bold", 
        "Borders", JSON ("Top", linetype_thin,"TopColor", "blue",  
            "Bottom", linetype_double, "BottomColor", "blue"))); 
    Excel_AddStyle (wb, "cdata", "data"); 
    Excel_SetStyleOptions ( wb, "cdata", JSON ("Alignment", "Top, center")); 

2.5.2. Excel_SetStyleOptions

Example: See example for Add style above.

2.5.3. Excel_GetStyleOptions

Example:

JSON style; 
style = Excel_GetStyleOptions(wb, "Ctitles")

Returns:

{
  "Font":"Areal",
  "FontSize": 16,
  "FillColor": "DDDDDD",
  "FillPattern": "solid",
  "Decoration": "Bold",
  "Borders": {
    "Bottom":"thin",
    "BottomColor":"000000"
  },
  "StyleName": "Ctitles"
}

2.5.4. Excel_ListStyles

Example:

// Add a sample sheet for the styles defined in the Workbook. 

    array string styles = Excel_ListStyles ( wb);
    int i, stylecount = sizeof ( styles ); 
    int s2 = Excel_add_Worksheet ( wb, "Sample Styles"); 

    array float cw2 = {10,50};
    Excel_SetColumnWidth (s2, cw2); 
    
    Excel_setCell ( s2, 1, 1, "Sample styles defined in this Workbook"); 
    for (i = 1, stylecount)
        Excel_setCell ( s2, i+3, 1, i); 
        Excel_setCell ( s2, i+3, 2, styles[i], styles[i]);
    end for
    outpath = substitute ( path, ".xlsx", "-edited.xlsx"); 
    excel_SaveAsWorkbook ( wb, outpath); 
    excel_close (wb); 
    return "OK";

2.5.5. Excel_CleanUnusedStyles

Example:

Excel_CleanUnusedStyles(wb);

2.6. Functions for Manipulating Rows and Columns

The functions for manipulating rows and columns do the following:

2.6.1. Excel_InsertRows

Example:

// Insert 3 rows below row 4
Excel_InsertRows (sheetID, 4, 3, false);

2.6.2. Excel_InsertColumns

Example:

// Insert 2 columns to the right of column 5
Excel_InsertColumns(sheetID, 5, 2, false);

2.6.3. Excel_DeleteRows

Example:

// Delete rows 10 to 12
Excel_DeleteRows (sheetID, 10, 12);

2.6.4. Excel_DeleteColumns

Example:

// Delete columns 10 to 12
Excel_DeleteColumns (sheetID, 10, 12);

// Delete all columns to the right of column 5
Excel_DeleteColumns (sheetID, 5, -1);

2.6.5. Excel_ShiftRight

2.6.6. Excel_ShiftLeft

2.6.7. Complete example functions to manipulate rows

Insert Rows and Delete Rows example

Here is a function to test the ExcelinsertRows and ExceldeleteRows functions:

function TestInsertAndDeleteRows (string path)
    string res, outpath; 
    if ( path == "" ) then
        return "No path"; 
    end if
    int wb = excel_create ( path, "Sheet1");
    Excel_SetMetaData (wb, JSON ("Author", "Me(Ole)","Created", now(), "Changed", now())); 
    res = ExcelSetDefaultStyles ( wb ); 
    int s = EXCEL_GetSheetID ( wb, 1); 
    Excel_setCell ( s, 1, 1, "Test Insert/Delete rows sheet"); 
    
    array int bb = {120,33,200,44,66,700,129,300,2000,18}; 
    int i, count = sizeof (bb); 
    
    for (i=1, count)
        Excel_setCell ( s, i+2, 1, bb[i], "dataNum2"); 
    end for
    
    // SumCell: 
    string formula = format ("SUM(%s)", excel_sheetaddress(3, 1, count+2,1));
    Excel_setCellFormula ( s, count+3, 1, formula, "SumLine"); 
    
    
    // Excel_InsertRows (int sheetID, int row {, int numberofrows {, bool insertabove}});
    excel_setCell ( s, 4, 2, "Row parameter, insert below this");
    excel_setCell ( s, 5, 2, "This should be moved 3 down.");
    
    Excel_InsertRows (s, 4, 3, false); 
    excel_setCell ( s, 11, 2, "Above deleted rows");
    excel_setCell ( s, 14, 2, "Below deleted rows");
    Excel_DeleteRows ( s, 12, 13); 
    
    Excel_CleanUnusedStyles (wb); 
    
    excel_close (wb); 
    return "OK"; 
end

Result:

insert-delete-example

Insert Columns and Delete Columns example

Here is another function to test Insert/delete columns:

function TestInsertAndDeleteColumns (string path)
    string res, outpath; 
    if ( path == "" ) then
        return "No path"; 
    end if
    int wb = excel_create ( path, "Sheet1");
    Excel_SetMetaData (wb, JSON ("Author", "Me(Ole)","Created", now(), "Changed", now())); 
    res = ExcelSetDefaultStyles ( wb ); 
    int s = EXCEL_GetSheetID ( wb, 1); 
    Excel_setCell ( s, 1, 1, "Test Insert/Delete Columns sheet"); 
    
    array int bb = {120,33,200,44,66,700,129,300,2000,18}; 
    int i, count = sizeof (bb); 
    string f; 
    Excel_setCell ( s, 3, count+1, "Line sum", "CH"); 
    for (i=1, count)
        Excel_setCell ( s, 3, i, "Col:"+string(i), "CH"); // Column headings are text cells
        Excel_setCell ( s, 4, i, bb[i], "dataNum2"); 
        Excel_setCell ( s, 5, i, bb[i]+100, "dataNum2"); 
        // Formulas below rows. 
        f = format ("SUM(%s)", excel_sheetaddress(4, i, 5,i));
        Excel_setCellFormula ( s, 6, i, f, "SumLine"); 
    end for
    
    // SumCell To the right of row 4 and 5: 
    string formula = format ("SUM(%s)", excel_sheetaddress(4, 1, 4,count));
    Excel_setCellFormula ( s, 4, count+1, formula, "SumLine"); 
    formula = format ("SUM(%s)", excel_sheetaddress(5, 1, 5,count));
    Excel_setCellFormula ( s, 5, count+1, formula, "SumLine"); 
    // Grand sum of the sums rightmost. 
    formula = format ("SUM(%s)", excel_sheetaddress(4, count+1, 5,count+1));
    Excel_setCellFormula ( s, 6, count+1, formula, "SumLine"); 
    // Excel_InsertColumns (int sheetID, int column {, int numberofcols {, bool insertleft}});
    
    excel_setCell ( s, 2, 5, "InsLeft");
    excel_setCell ( s, 2, 6, "InsRight");
    
    Excel_InsertColumns (s, 5, 2, false); 
    
    excel_setCell ( s, 2, 9, "DelLeft");
    excel_setCell ( s, 2, 12, "DelRight");
    Excel_DeleteColumns ( s, 10, 11); 
    
    // Move table 1 column right to make room for the line descriptions. 
    Excel_InsertColumns (s, 1, 1, true); // Insert column far left. 
    excel_setCell ( s, 4, 1, "Items-1");
    excel_setCell ( s, 5, 1, "Items-2");
    excel_setCell ( s, 6, 1, "Sum", "SumLine");
    excel_setColumnWidth ( s, 1, 20); // Make it twize as wide (def = 10)
    Excel_CleanUnusedStyles (wb); 
    
    excel_close (wb); 
    return "OK"; 
end

Here is the result:

inseret-delete-columns

2.7. Helper Functions

2.7.1. Excel_SheetAddress

Formulas in Excel spreadsheets use Excel style address. To create those dynamically, this function becomes handy.

Example:

string formula1 = Excel_SheetAddress ( 2,7 ) // G2
string formula2 = Excel_SheetAddress ( 2,7, 5,7 ) // G2:G5
string formula3 = Excel_SheetAddress ( "Sheet1", 2,7 ) // 'Sheet1'!G2
string formula4 = Excel_SheetAddress ( "Sheet2", 2,7, 5,7 ) // 'Sheet2'!G2:G5

2.8. Excel_ID and Sheet_ID

Excel_ID identifies the workbook, while SheetID identifies a sheet within a workbook. The ID's are to some degrees interchangeable, since both contains the Excel_ID.

This means that all commands that require an Excel_ID will work with an Sheet_ID too.