Example #1
0
/*
 * Convert sheetname and zero indexed row and column pairs to an Excel style
 * Sheet1!$A$1:$C$5 formula reference with absolute values.
 */
void
lxw_rowcol_to_formula_abs(char *formula, const char *sheetname,
                          lxw_row_t first_row, lxw_col_t first_col,
                          lxw_row_t last_row, lxw_col_t last_col)
{
    size_t pos;
    char *quoted_name = lxw_quote_sheetname(sheetname);

    strncpy(formula, quoted_name, LXW_MAX_FORMULA_RANGE_LENGTH - 1);
    free(quoted_name);

    /* Get the end of the sheetname. */
    pos = strlen(formula);

    /* Add the range separator. */
    formula[pos++] = '!';

    /* Add the first cell to the range. */
    lxw_rowcol_to_cell_abs(&formula[pos], first_row, first_col, 1, 1);

    /* If the start and end cells are the same just return a single cell. */
    if (first_row == last_row && first_col == last_col)
        return;

    /* Get the end of the cell. */
    pos = strlen(formula);

    /* Add the range separator. */
    formula[pos++] = ':';

    /* Add the first cell to the range. */
    lxw_rowcol_to_cell_abs(&formula[pos], last_row, last_col, 1, 1);
}
// Test _datetime_to_excel_date().
CTEST(utility, _quote_sheetname) {

    ASSERT_STR("Sheet1",     lxw_quote_sheetname("Sheet1"));
    ASSERT_STR("Sheet.2",    lxw_quote_sheetname("Sheet.2"));
    ASSERT_STR("Sheet_3",    lxw_quote_sheetname("Sheet_3"));
    ASSERT_STR("'Sheet4'",   lxw_quote_sheetname("'Sheet4'"));
    ASSERT_STR("'Sheet 5'",  lxw_quote_sheetname("Sheet 5"));
    ASSERT_STR("'Sheet!6'",  lxw_quote_sheetname("Sheet!6"));
    ASSERT_STR("'Sheet''7'", lxw_quote_sheetname("Sheet'7"));
    ASSERT_STR("'a''''''''''''''''''''''''''''''''''''''''''''''''''''''''''b'",
               lxw_quote_sheetname("a'''''''''''''''''''''''''''''b"));
}