diff --git a/funcext/TSOffice/document/TSWordTemplateMethods.tsf b/funcext/TSOffice/document/TSWordTemplateMethods.tsf new file mode 100644 index 0000000..58e032a --- /dev/null +++ b/funcext/TSOffice/document/TSWordTemplateMethods.tsf @@ -0,0 +1,297 @@ +Unit TSWordTemplateMethods; +Interface + Function ExcelChartCopyPic(excelFileName, excelSheetName, chartName, width, height); + Function WordAddOleObjectFromFile(fileName, width, height); + Function WordBr(); + Function WordCopyFromExcel(excelFileName, excelSheetName, startRow, startCol, endRow, endCol); + Function WordGetCurrentDocument(); + Function WordGetCurrentRange(); + Function WordGetFont(prop, value); + Function WordGetLastRange(); + Function WordGetLastShape(); + Function WordGetLastTable(); + Function WordGetLastTableCellProp(row, col, name, value); + Function WordGetLastTableProp(name, value); + Function WordLastTableMerge(row1, col1, row2, col2); + Function WordSetCurrentRange(range); + Function WordSetFont(prop, value); + Function WordSetLastTableCellProp(row, col, name, value); + Function WordSetLastTableProp(name, value); + Function WordSetRangeProp(name, value); + Function WordTemplate(data, template, fileName); + Function WordWrite(); + Function WordWritePicFromFile(fileName); + Function WordWriteTable(table, writeIntIndex, writeIntTitle, incIntIndex); + +Implementation + Function ExcelChartCopyPic(excelFileName, excelSheetName, chartName, width, height); + Begin + docx := TOfficeApi().GetDocument(); + chart := docx.CopyExcelChart(excelFileName, excelSheetName, chartName, width, height, TOfficeApi().GetCurrentPosition()); + return ifObj(chart); + End; + + Function WordBr(); + Begin + TOfficeApi().GetDocument().AddLineBreak(TOfficeApi().Get('CurrentPosition')); + End; + + Function WordCopyFromExcel(excelFileName, excelSheetName, startRow, startCol, endRow, endCol); + Begin + excel := new TSXlsFile(); + [err, msg] := excel.OpenFile('', excelFileName); + if err then return false; + total_row := excel.TotalRows(excelSheetName); + total_col := excel.TotalCols(excelSheetName); + if not total_row or not total_col then return false; + if ifnil(startRow) then + startRow := 1; + if ifnil(startCol) then + startCol := 1; + if ifnil(endRow) then + endRow := total_row; + if ifnil(endCol) then + endCol := total_col; + data := excel.GetTable(excelSheetName, + CoordinatesToCellName(startCol, startRow), + CoordinatesToCellName(endCol, endRow), + false, + false, + false); + if length(data) = 0 then return false; + //println('data={}',data); + docx := TOfficeApi().GetDocument(); + tbl := docx.CreateTable(data, false, false); + tbl.Format.Borders.Top.Val := 'dashed'; + tbl.Format.Borders.Left.Val := 'dashed'; + tbl.Format.Borders.Bottom.Val := 'dashed'; + tbl.Format.Borders.Right.Val := 'dashed'; + tbl.Format.Borders.InsideH.Val := 'dashed'; + tbl.Format.Borders.InsideV.Val := 'dashed'; + tbl := docx.InsertTable(tbl, TOfficeApi().GetCurrentPosition()); + End; + + Function WordGetCurrentDocument(); + Begin + return TOfficeApi().GetDocument(); + End; + + Function WordGetCurrentRange(); + Begin + return TOfficeApi().GetCurrentPosition(); + End; + + Function WordGetFont(prop, value); + Begin + crun := TOfficeApi().GetCurrentRun(); + node := crun.Node().FirstChildElement('w:rPr'); + rPr := TOfficeObj('TwrPr'); + rPr.InitRootNode(node); + value := rPr.Value(prop); + if not ifnil(value) then return true; + return false; + End; + + Function WordGetLastShape(); + Begin + node := TOfficeApi().Get('CurrentShape'); + if not ifObj(node) then return nil; + uri := 'w:r/w:drawing/wp:inline/a:graphic/a:graphicData/c:chart'; + cNode := class(TSXml).GetNode(node, uri); + if ifObj(cNode) then Begin + chart := TOfficeObj('TChart'); + chart.Init(TOfficeApi().GetDocument(), node, cNode); + return chart; + End; + + pic := TOfficeObj('TPicture'); + pic.Init(node); + return pic; + End; + + Function WordGetLastRange(); + Begin + return TOfficeApi().GetCurrentPosition(); + End; + + Function WordGetLastTable(); + Begin + return TOfficeApi().Get('CurrentTable'); + End; + + Function WordGetLastTableCellProp(row, col, name, value); + Begin + table_node := TOfficeApi().Get('CurrentTable'); + if ifnil(table_node) then return false; + table := TOfficeObj("TTable"); + table.Init(table_node); + table_cell := table.Cell(row, col); + ctr := _wordCompTable(); + name_ := ctr[lowercase(name)]; + if ifnil(name_) then name_ := name; + str := 'return table_cell.Format.' $ name_ $ '"' $ value $ '"'; + value := eval(&str); + return true; + End; + + Function WordGetLastTableProp(name, value); + Begin + table_node := TOfficeApi().Get('CurrentTable'); + if ifnil(table_node) then return false; + table := TOfficeObj("TTable"); + table.Init(table_node); + ctr := _wordCompTable(); + name_ := ctr[lowercase(name)]; + if ifnil(name_) then name_ := name; + str := 'return table.Format.' $ name_ $ '"' $ value $ '"'; + value := eval(&str); + return true; + End; + + Function WordLastTableMerge(row1, col1, row2, col2); + Begin + table_node := TOfficeApi().Get('CurrentTable'); + if ifnil(table_node) then return false; + table := TOfficeObj("TTable"); + table.Init(table_node); + [err1, beg_cell] := CoordinatesToCellName(col1, row1); + [err2, end_cell] := CoordinatesToCellName(col2, row2); + if err1 or err2 then return false; + table.Merge(beg_cell, end_cell, false); + return true; + End; + + Function WordSetCurrentRange(range); + Begin + TOfficeApi().Set('CurrentPosition', range); + End; + + Function WordSetFont(prop, value); + Begin + run := TOfficeApi().GetCurrentRun(); + str := 'run.font.' $ prop $ ' := ' $ '"' $ value $ '"'; + eval(&str); + run.Apply(); + return true; + End; + + Function WordSetLastTableCellProp(row, col, name, value); + Begin + table := TOfficeApi().Get('CurrentTable'); + if ifnil(table) then return false; + table_cell := table.Cell(row, col); + ctr := _wordCompTable(); + name_ := ctr[lowercase(name)]; + if ifnil(name_) then name_ := name; + str := 'table_cell.Format.' $ name_ $ ' := '; + if ifstring(value) then + str += '"' $ value $ '"'; + else + str := str $ value; + eval(&str); + return true; + End; + + Function WordSetLastTableProp(name, value); + Begin + table := TOfficeApi().Get('CurrentTable'); + if ifnil(table) then return false; + ctr := _wordCompTable(); + name_ := ctr[lowercase(name)]; + if ifnil(name_) then name_ := name; + str := 'table.Format.' $ name_ $ ' := '; + if ifstring(value) then + str += '"' $ value $ '"'; + else + str := str $ value; + eval(&str); + TOfficeApi().Set('CurrentTable', table); + return true; + End; + + Function WordSetRangeProp(name, value); + Begin + cur := TOfficeApi().GetCurrentPosition(); + if ifnil(cur) then return nil; + End; + + Function WordTemplate(data, template, fileName); + Begin + setsysparam('data', data); + docx := new TSDocxFile(); + [err, msg] := docx.OpenFile('', template); + if err then return false; + docx.ExecInnerTSL(); + if ifstring(fileName) then + begin + [err, msg] := docx.SaveAs('', fileName); + if err then return false; + end + return true; + End; + + Function WordWrite(); + Begin + docx := TOfficeApi().GetDocument(); + curParagraph := TOfficeApi().GetCurrentParagraph(); + curRun := TOfficeApi().GetCurrentRun(); + for i:=1 to PARAMCOUNT do Begin + argv := params[i]; + if ifstring( argv ) then + curRun.AddText( argv ); + else if ifnumber(argv) then + curRun.AddText('' $ argv); + else if ifarray(argv) then Begin + tbl := docx.CreateTable(argv, false, false); + tbl := docx.InsertTable(tbl, TOfficeApi().GetCurrentPosition()); + End + End + End; + + Function WordWritePicFromFile(fileName); + Begin + file_size := fileSize('', fileName); + flag := readfile(rwbinary(), '', fileName, 0, file_size, data); + if not flag then return false; + picture := TOfficeObj('TPicture'); + picture.Image := data; + docx := TOfficeApi().GetDocument(); + docx.AddPicture(picture, TOfficeApi().GetCurrentPosition()); + return true; + End; + + Function WordWriteTable(table, writeIntIndex, writeIntTitle, incIntIndex); + Begin + docx := TOfficeApi().GetDocument(); + tbl := docx.CreateTable(table, true, writeIntIndex); + table := docx.InsertTable(tbl, TOfficeApi().GetCurrentPosition()); + return true; + End; + + Function _wordCompTable(); + Begin + return array( + 'alignment': 'Alignment', + 'shadingcolor': 'Shading.Color', + 'style': 'StyleID', + 'verticalalignment': 'Valign', + 'width': 'Width', + ); + End; + + // TODO:尚未支持 + Function WordAddOleObjectFromFile(fileName, width, height); + Begin + End; + Function WordCopyPicFormatExcel(excelFileName, excelSheetName, startRow, startCol, endRow, endCol, picType); + Begin + End; + Function WordGetRangeProp(name, value); + Begin + End; + Function WordLastTableSplit(row, col, rCnt, cCnt); + Begin + End; + +End. +