ACF Library

InsertNewRow

Back to List

Description: Insert new row in CashFlow budget demo application

FileMaker Prototype:

Set Variable [$res; ACF_Run("InsertNewRow"; int_s;  int_first;  int_last;  string_name;  string_style)]

Category: EXCEL

Function source:

// Functi on: InsertNewRow
// Description: Inserts a new row into the expense or income section of a sheet, 
//              determined by the first and last rows. The new row is placed in 
//              alphabetical order based on the label in column A.
// Parameters:
//   - s: The sheet identifier (integer).
//   - first: The first row of the section where the new row may be inserted.
//   - last: The last row of the section where the new row may be inserted.
//   - name: The label (string) for the new row to insert.
//   - style: The style (string) to apply to the new label.
// Returns:
//   - The row number (integer) where the new row was inserted.

function InsertNewRow(int s, int first, int last, string name, string style)
    // Validate input: Ensure first and last rows are valid
    if (first < 0 || last < 0) then
        throw "First or last row in insert calculation is negative row numbers.";
    end if

    string uNameFormSheet, uName = upper(name); // Convert the label to uppercase for comparison
    int i, r = -1;                             // Initialize variables
    bool before = true;                        // Flag to indicate insertion direction (above/below)

    // Determine the insertion position based on alphabetical order
    for (i = first, last)
        uNameFormSheet = upper(Excel_GetCell(s, i, 1)); // Get and normalize the label in column A
        if (uNameFormSheet > uName) then
            r = i; // Found the correct position to insert
            break;
        end if
    end for

    // If no position was found, append the row after the last row
    if (r == -1) then
        r = last;
        before = false;
    end if

    // Insert a new row at the determined position
    excel_insertRows(s, r, 1, before);

    // Adjust row number if the row was inserted after the last row
    if (!before) then
        r = r + 1;
    end if

    // Set the label text for the new row and apply the specified style
    Excel_SetCell(s, r, 1, name, style);

    // Return the row number of the newly inserted row
    return r;
end

Back to List