Spreadsheet Cell Formatting

夙愿已清 提交于 2019-11-29 12:24:00

I suspect you may have run into a bug with <cfspreadsheet action="update" ..>.

When CF performs the "update" it attempts to copy everything from sheet "a" (values, formats, formulas, etcetera) into a new sheet within the workbook you saved on disk. Apparently CF is not copying everything. Hence the missing formats. You can prove this by saving sheet "a"(only) to a separate file. Notice when you use action="write" the cell formats are correct (red and green appear)?

  ...
  <!--- save separate copy of "ThisSheet" only --->
  <cfspreadsheet action="write" filename="c:/thisSheetOnly.xls" name="ThisSheet"  ....>
  <!--- attempt to combine the sheets --->
  <cfspreadsheet action="update" filename="#MyFile#" name="ThisSheet" ... >



Honestly, creating a complete copy of an entire worksheet is tricky. There are a lot of moving pieces and it is easy to miss something, which is what seems to be happening here. Personally, I would avoid using action="update" if at all possible. Too many things can go wrong. Besides, in most cases you do not need it. You can easily create a workbook, then add and populate multiple sheets. Here is a simplified example that creates and saves two sheets, then saves them to disk. (Tested with CF10)

<cfscript>
    // Create new sheet and add one row
    Workbook = Spreadsheetnew("Sheet1");
    SpreadSheetAddRow(Workbook, "fred");

    // Apply formatting to new cell 
    format = {bold = true, color = "blue"};
    SpreadsheetFormatCell(Workbook, Format, 1, 1); 
    WriteDump(format);

    //Add another worksheet and make it active
    letter = "a";
    SpreadSheetCreateSheet(Workbook, letter);
    SpreadSheetSetActiveSheet(Workbook, letter);

    //Add rows to the active worksheet
    RowNumber = 1;
    Values = "a,b,a,b";
    for (i = 1; i <= 4; i ++) {
        SpreadsheetAddRow(Workbook, ListGetAt(Values, i), RowNumber, 1);
        if (ListGetAt(Values, i) == "a") {
            Format = {bold = true, color = "green"};
            SpreadsheetFormatCell(Workbook, Format, RowNumber, 1); 
            WriteDump(var=format, label="RowNumber="& RowNumber);
        }
        else {
            Format = {bold = true, color = "red"};
            SpreadsheetFormatCell(Workbook, Format, RowNumber, 1); 
            WriteDump(var=format, label="RowNumber="& RowNumber);
        }
        RowNumber++;   
    }

    // Set the active worksheet back to the original.  If you don't 
    // the last worksheet name will be the name of the spreadsheet
    // object, in this case, workbook.
    SpreadSheetSetActiveSheetNumber(Workbook, 1);

    //Finally, save it to disk
    SpreadSheetWrite(Workbook, "c:/path/to/yourFile.xls", true);
</cfscript>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!