Forum > FPvectorial
[SOLVED] Adding columns to a Word document table (FPVectorial)
Mike.Cornflake:
> There does not seem to be any way to add several columns to a table.
Add multiple cells to a row. Go on, try it :-) As I said before, looking at docxwriter, this should be sufficient.
JD:
--- Quote from: Mike.Cornflake on April 16, 2019, 08:44:52 pm ---> There does not seem to be any way to add several columns to a table.
Add multiple cells to a row. Go on, try it :-) As I said before, looking at docxwriter, this should be sufficient.
--- End quote ---
I tried it but the resulting Word document is corrupted and cannot be opened. I added a second Cell := Row.AddCell; a line Cell2 := Row.AddCell; a fluent programming style with Cell := Row.AddCell.Row.AddCell; It all created unreadable Word documents
This is what I'm trying to do. I am trying to create a work schedule document with 2 columns: Days & Hours Worked.
The code I'm using is as follows (of course in the real world, the hours will be input manually):
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var Document: TvVectorialDocument; Page: TvTextPageSequence; BoldTextStyle: TvStyle; HeaderParagraph, Paragraph: TvParagraph; DaysTable: TvTable; Row: TvTableRow; Cell, Cell2: TvTableCell; intRow, intTotalRows: integer; DaysArray, HoursWorkedArray: array of string;begin // DaysArray := ['DAYS', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; HoursWorkedArray := ['HOURS WORKED', '4', '7', '8', '8', '4', '8', '0']; // Create the document Document := TvVectorialDocument.Create; // try // Adds the defaut Paragraph Styles // StyleTextBody, StyleHeading1, // StyleHeading2 & StyleHeading3 Document.AddStandardTextDocumentStyles(vfUnknown); // Add our own Style BoldTextStyle := Document.AddStyle(); BoldTextStyle.Kind := vskTextSpan; BoldTextStyle.Name := 'Bold'; BoldTextStyle.Font.Bold := True; BoldTextStyle.SetElements := BoldTextStyle.SetElements + [spbfFontBold]; // Add a page and a paragraph Page := Document.AddTextPageSequence; // HeaderParagraph := Page.AddParagraph; HeaderParagraph.Style := Document.StyleHeading1Centralized; HeaderParagraph.AddText('WORK SCHEDULE'); HeaderParagraph := Page.AddParagraph; // Add the Days table DaysTable := Page.AddTable; DaysTable.PreferredWidth := Dimension(100, dimPercent); DaysTable.ColWidthsUnits := dimMillimeter; // Get the number of rows to add to the table intTotalRows := Length(DaysArray); // for intRow := 0 to Pred(intTotalRows) do begin Row := DaysTable.AddRow; if intRow = 0 then begin Row.BackgroundColor := RGBToFPColor(192, 192, 192); // Grey Shading Row.Header := True; // Tell the table this is a Header Row end; Cell := Row.AddCell; Paragraph := Cell.AddParagraph; Paragraph.Style := Document.StyleTextBody; Paragraph.AddText(DaysArray[intRow]).Style := BoldTextStyle; end; try // Save the document Document.WriteToFile('Work schedule.docx', vfDOCX); except raise; end; finally Document.Free; end;end;
The resulting Microsoft Word document created from the code above is in the attachment. This is where I am as of today. Any help will be greatly appreciated.
Thanks,
JD
Mike.Cornflake:
Yup - as you say - it's broken. I've confirmed the full write test I wrote no longer works.
Here's the original function
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TvTable.AddColWidth(AValue: Double): Integer;begin SetLength(ColWidths, Length(ColWidths) + 1); ColWidths[High(ColWidths)] := AValue;end;
ColWidths is an array of double, and it's in TvTable.Public. So you can implement your own AddColWidth and see if that produces a working docx :-(
Sorry about this :-(
Mike
UPDATE:
In order to get fpvtextwritetest2 to run, I added the following helper routine.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- Procedure AddColWidth(ATable: TvTable; AValue: Double); begin SetLength(ATable.ColWidths, Length(ATable.ColWidths) + 1); ATable.ColWidths[High(ATable.ColWidths)] := AValue; end;
I then changed all calls from
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- CurTable.AddColWidth( 15); CurTable.AddColWidth(20); CurTable.AddColWidth(20); CurTable.AddColWidth(20); CurTable.AddColWidth(79.5); // For Word (and possibly odt), this only has to be close.
to
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- AddColWidth(CurTable, 15); AddColWidth(CurTable, 20); AddColWidth(CurTable, 20); AddColWidth(CurTable, 20); AddColWidth(CurTable, 79.5); // For Word (and possibly odt), this only has to be close.
Result Word Document was well formed and worked.
When I'm back on land I'll submit a patch and get the original code restored. Either that or get the author of revision 55389 to update the wiki. My preference is original code restored.
JD:
@Mike.Cornflake
No need to be sorry.
I added a class helper to my code as follows:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type { TvTableHelper } TvTableHelper = class helper for TvTable public function AddColWidth(AValue: Double): Integer; end; { TvTableHelper } function TvTableHelper.AddColWidth(AValue: Double): Integer;begin SetLength(ColWidths, Length(ColWidths) + 1); ColWidths[High(ColWidths)] := AValue;end;
My new document creation code now becomes:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var Document: TvVectorialDocument; Page: TvTextPageSequence; BoldTextStyle: TvStyle; HeaderParagraph, Paragraph: TvParagraph; DaysTable: TvTable; Row: TvTableRow; Cell, Cell2: TvTableCell; intRow, intTotalRows: integer; DaysArray, HoursWorkedArray: array of string;begin // DaysArray := ['DAYS', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; HoursWorkedArray := ['HOURS WORKED', '4', '7', '8', '8', '4', '8', '0']; // Create the document Document := TvVectorialDocument.Create; // try // Adds the defaut Paragraph Styles // StyleTextBody, StyleHeading1, // StyleHeading2 & StyleHeading3 Document.AddStandardTextDocumentStyles(vfUnknown); // Add our own Style BoldTextStyle := Document.AddStyle(); BoldTextStyle.Kind := vskTextSpan; BoldTextStyle.Name := 'Bold'; BoldTextStyle.Font.Bold := True; BoldTextStyle.SetElements := BoldTextStyle.SetElements + [spbfFontBold]; // Add a page and a paragraph Page := Document.AddTextPageSequence; // HeaderParagraph := Page.AddParagraph; HeaderParagraph.Style := Document.StyleHeading1Centralized; HeaderParagraph.AddText('WORK SCHEDULE'); HeaderParagraph := Page.AddParagraph; // Add the Days table DaysTable := Page.AddTable; DaysTable.PreferredWidth := Dimension(100, dimPercent); DaysTable.ColWidthsUnits := dimMillimeter; // Add 2 columns DaysTable.AddColWidth(50); DaysTable.AddColWidth(50); // Get the number of rows to add to the table intTotalRows := Length(DaysArray); // for intRow := 0 to Pred(intTotalRows) do begin Row := DaysTable.AddRow; if intRow = 0 then begin Row.BackgroundColor := RGBToFPColor(192, 192, 192); // Grey Shading Row.Header := True; // Tell the table this is a Header Row end; Cell := Row.AddCell; Paragraph := Cell.AddParagraph; Paragraph.Style := Document.StyleTextBody; Paragraph.AddText(DaysArray[intRow]).Style := BoldTextStyle; end; try // Save the document Document.WriteToFile('Work schedule.docx', vfDOCX); except raise; end; finally Document.Free; end;end;
But it stll creates a table with one column!! What is it that I am missing?
Thanks a lot for your assistance,
JD
JD:
I didn't see the updates you added to your last post. I'll try them now.....
Navigation
[0] Message Index
[#] Next page
[*] Previous page