TOfficeApi().CodePage('中文');

testCount := 0;
sheetName := "你好";
sheetName2 := "中文";

// OpenFile
excel := new TSExcelFile();
[err, errmsg] := excel.OpenFile('', 'd:\\temp\\b.xlsx');
PrintInfo("OpenFile", err, errmsg);

// Save
[err, errmsg] := excel.Save();
PrintInfo("Save", err, errmsg);

excel := new TSExcelFile();
// NewFile
[err, errmsg] := excel.NewFile();
PrintInfo("NewFile", err, errmsg);

// SaveAs
[err, errmsg] := excel.SaveAs('', 'd:\\temp\\test.xlsx');
PrintInfo("SaveAs", err, errmsg);

// FileName
name := excel.FileName();
println("[success] name = {}", name);

// GetSheetsCount
sheetCount := excel.GetSheetsCount();
println("[success] GetSheetsCount = {}", sheetCount);

// SetSheetName
excel.SetSheetName("sheet1", sheetName);
println("[success] SetSheetName");

// GetSheetName
Name := excel.GetSheetName(0);
println("[success] GetSheetName = {}", Name);

// NewSheet
excel.NewSheet(sheetName2);
println("[success] NewSheet");

// NewSheet
excel.NewSheet(sheetName, 'sheet_copy');
println("[success] NewSheet2");

// InsertSheet
excel.InsertSheet(sheetName, sheetName + 'before');
println("[success] NewSheet2");

// GetSheets
sheets := excel.GetSheets();
println("[success] sheets = {}", sheets);

// DeleteSheet
excel.NewSheet("test1");
excel.DeleteSheet("test1");
println("[success] DeleteSheet");

// SetCellValue
excel.SetCellValue(sheetName, "A1", 100);
excel.SetCellValue(sheetName, "F1", "中文的壹佰");
println("[success] SetCellValue");

// GetCellValue
[err, errmsg] := excel.GetCellValue(sheetName, "A1");
PrintInfo("GetCellValue A1 ", err, errmsg);
[err, errmsg] := excel.GetCellValue(sheetName, "F1");
PrintInfo("GetCellValue F1 ", err, errmsg);

// TotalCols
totalCols := excel.TotalCols(sheetName);
println("[success] TotalCols = {}", totalCols);

// TotalRows
totalRows := excel.TotalRows(sheetName);
println("[success] TotalRows = {}", totalRows);

// SetCellRichText
richText := TOfficeObj('TRichtext');
text1 := richText.AddText('Hello 你好');
text1.Font.Size := 35;
text1.Font.Color := "FFF000";
text2 := richText.AddText("  World");
text2.Font.Size := 25;
text2.Font.Bold := true;
excel.SetCellRichText(sheetName, "A2", richText);
text2.Text := "python";
excel.SetCellRichText(sheetName, "A3", richText);

// GetCellRichText
arr := excel.GetCellRichText(sheetName, "A2");
println("[success] GetCellRichText = {}", arr);

// ClearCell
excel.ClearCell(sheetName, "A1");
//excel.ClearCell(sheetName);
println("[success] ClearCell");

// SetCellFormula
excel.SetCellFormula(sheetName, "A2", "100*50");
println("[success] SetCellFormula");

// GetCellFormula
[err, errmsg] := excel.GetCellFormula(sheetName, "A2");
PrintInfo("GetCellFormula", err, errmsg);

// SetSheetRow
excel.SetSheetRow(sheetName, "C3", array(1, 2, 3, 4, "a", "b"));
println("[success] SetSheetRow");

// InsertTable
table := array((1, 2, "你好"), (4, 5, "b"));
//table2 := array(("title1" :1, "title2": 2), ("title1": 3, "title2": 4));
[err, errmsg] := excel.InsertTable(sheetName, "A5", table);
PrintInfo("InsertTable", err, errmsg);

// GetTable
table := excel.GetTable(sheetName, "A5", "B7");
println("[success] GetTable = {}", table);

// InsertCol
excel.InsertCol(sheetName, "B");
excel.InsertCol(sheetName, "B");
println("[success] InsertCol");

// InsertRow
excel.InsertRow(sheetName, 3);
excel.InsertRow(sheetName, 3);
println("[success] InsertRow");

// RemoveCol
excel.RemoveCol(sheetName, "B");
println("[success] RemoveCol");

// RemoveRow
excel.RemoveRow(sheetName, 3);
println("[success] RemoveRow");

// SetRowHeight
excel.SetRowHeight(sheetName, 1, 30);
excel.SetRowHeight(sheetName, 5, 30);
println("[success] SetRowHeight");

// GetRowHeight
height := excel.GetRowHeight(sheetName, 2);
println("[success] GetRowHeight = {}", height);
height := excel.GetRowHeight(sheetName, 5);
println("[success] GetRowHeight = {}", height);

// SetColWidth
excel.SetColWidth(sheetName, "A", "D", 20);
println("[success] SetColWidth");

// GetColWidth
width := excel.GetColWidth(sheetName, "A");
println("[success] GetColWidth = {}", width);
width := excel.GetColWidth(sheetName, "E");
println("[success] GetColWidth = {}", width);

// SetSheetDefaultColWidth
excel.SetSheetDefaultColWidth(sheetName, 15);
println("[success] SetSheetDefaultColWidth");

// GetSheetDefaultColWidth
width := excel.GetSheetDefaultColWidth(sheetName);
println("[success] GetSheetDefaultColWidth = {}", width);

// AddComment
excel.AddComment(sheetName, "A1", "csh", "heiheihei");
println("[success] AddComment");

[author, comment] := excel.GetComment(sheetName, 'A1');
println("[success] GetComment  author = {}, comment = {}", author, comment);

// AddChart
data := array(("Name":"Small","Apple":2,"Orange":3,"Pear":3),
	("Name":"Normal","Apple":5,"Orange":2,"Pear":4),
	("Name":"Large","Apple":6,"Orange":7,"Pear":8));
chart := TOfficeObj('TChart');
chart.Type := 'col';
chart.VaryColors := true;
chart.BegColOff := 20000;
chart.BegRowOff := 20000;
chart.Name := 'test chart 1';
chart.Title := 'Hans Chart';

chart.AddSeries('Sheet1!$A$6', 'Sheet1!$B$5:$D$5', 'Sheet1!$B$6:$D$6');
chart.AddSeries('Sheet1!$A$7', 'Sheet1!$B$5:$D$5', 'Sheet1!$B$7:$D$7');
chart.AddSeries('Sheet1!$A$8', 'Sheet1!$B$5:$D$5', 'Sheet1!$B$8:$D$8');

chart.ShowLegendKey := false;
chart.Legend.Position := 'left';

chart.ShowBubbleSize := true;
chart.ShowCatName := false;
chart.ShowLeaderLines := false;
chart.ShowPercent := true;
chart.ShowSerName := true;
chart.ShowVal := true;
chart.ShowBlanksAs := 'zero';
	
excel.NewSheet('Sheet1');
[err, errinfo] := excel.InsertTable("Sheet1","A5",data, true);
chart1 := excel.AddChart('Sheet1', 'A11:G30', chart);
println("[success] AddChart");

// GetCharts
[err, charts] := excel.GetCharts('Sheet1');
for i:=0 to length(charts)-1 do Begin
	println('ChartName={}, Title={}, Series={}',charts[i].Chart.Name, charts[i].Chart.Title, charts[i].Chart.Series);
End;

// SplitCellName
[err, col, row] := excel.SplitCellName("AK47");
if not err then println("[success] {} = col : {}, row = {}", "SplitCellName", col, row);

// ColumnNameToNumber
[err, errmsg] := excel.ColumnNameToNumber("AK");
PrintInfo("ColumnNameToNumber", err, errmsg);

// ColumnNumberToName
[err, errmsg] := excel.ColumnNumberToName(37);
PrintInfo("ColumnNumberToName", err, errmsg);

// CellNameToCoordinates
[err, col, row] := excel.CellNameToCoordinates("A2");
if not err then println("[success] {} = col : {}, row = {}", "CellNameToCoordinates", col, row);

// CoordinatesToCellName
[err, cell] := excel.CoordinatesToCellName(1, 2, true);
println("[success] CoordinatesToCellName, cell = {}", cell);
[err, cell] := excel.CoordinatesToCellName(1, 2);
println("[success] CoordinatesToCellName, cell = {}", cell);

// RGBToHSL
[h, s, l] := excel.RGBToHSL(255, 15, 33);
println("[success] {} = {}, {}, {}", "RGBToHSL", h, s, l);

// HSLToRGB
[r, g, b] := excel.HSLToRGB(h, s, l);
println("[success] {} = {}, {}, {}", "HSLToRGB", r, g, b);

// NewStyle
style := TOfficeObj('TStyle');
style.Font.Color := "A020F0";
style.Font.Size := 22;
style.Font.Name := "黑体";
style.Font.Bold := True;
style.Font.Charset := "134";
style.Font.Script := "subscript";
style.Font.Underline := "double";
style.Font.Strikethrough := True;
style.Font.Italic := True;
style.Alignment.Horizontal := "center";
style.Alignment.Vertical := "justify";
style.Alignment.WrapText := True;
style.Alignment.ReadingOrder := 2;
style.Alignment.TextRotation := 45;
style.Border.Left.LineStyle := "thin";
style.Border.Left.Color := "A020F0";
style.Border.Right.LineStyle := "thin";
style.Border.Right.Color := "FF0000";
style.Border.Bottom.LineStyle := "mediumDashed";
style.Border.Bottom.Color := "FF0000";
style.Border.Top.LineStyle := "mediumDashed";
style.Border.Top.Color := "FF0000";
style.Border.DiagonalDown := True;
style.Border.DiagonalUp := True;
style.Border.Diagonal.LineStyle := "double";
style.Border.Diagonal.Color := "A020F0";
style.NumberFormat.FormatCode := "#,##0.000_ ";
//style.Fill.Pattern.PatternType := "solid";
//style.Fill.Pattern.ForegroundColor := "FF6699FF";
//style.Fill.Pattern.BackgroundColor := "FFFF0000";
style.Fill.Gradient.Degree := 270;
style.Fill.Gradient.Stop1.Position := 0;
style.Fill.Gradient.Stop1.ThemeColor := 0;
style.Fill.Gradient.Stop2.Position := 1;
style.Fill.Gradient.Stop2.ThemeColor := 4;
style.Protection.Hide := 1;
style.Protection.Lock := 1;

styleid1 := excel.NewStyle(style);
println("[success] NewStyle = {}", styleid1);

// SetCellStyle
excel.SetCellStyle(sheetName, "A1", "A2", styleid1);
println("[success] SetCellStyle");

styleid := excel.GetCellStyle(sheetName, "A1");
println("[success] GetCellStyle = {}", styleid);

style := excel.GetStyle(styleid);
println("font.Name = {}", style.Font.Name);
println("font.Italic = {}", style.Font.Italic);
println("border.left.Color = {}", style.border.left.Color);
println("border.Top.LineStyle = {}", style.border.Top.LineStyle);
println("border.DiagonalDown = {}", style.border.DiagonalDown);
println("numberFormat.FormatCode = {}", style.NumberFormat.FormatCode);
println("alignment.vertical = {}", style.Alignment.Vertical);
println("alignment.TextRotation = {}", style.Alignment.TextRotation);
println("protection.Hide = {}", style.Protection.Hide);
println("protection.Lock = {}", style.Protection.Lock);
//println("fill.pattern.PatternType = {}", style.Fill.Pattern.PatternType);
//println("fill.pattern.ForegroundColor = {}", style.Fill.Pattern.ForegroundColor);
println("style.Fill.Gradient.Degree = {}", style.Fill.Gradient.Degree);
println("style.Fill.Gradient.Stop1.ThemeColor = {}", style.Fill.Gradient.Stop1.ThemeColor);
println("style.Fill.Gradient.Stop2.Position = {}", style.Fill.Gradient.Stop2.Position);

// SetSheetHeaderFooter
headerFooter := TOfficeObj("THeaderFooter");
headerFooter.DifferentOddEven := true;
headerFooter.DifferentFirst := true;
headerFooter.ScaleWithDoc := False;
headerFooter.AlignWithMargin := False;
headerFooter.OddHeader := "&L123&456&789";
headerFooter.OddFooter := "&Ldef&Rhij";
headerFooter.EvenHeader := "&L123&R456";
headerFooter.EvenFooter := "&L456&R789";
headerFooter.FirstHeader := "&L++";
headerFooter.FirstFooter := "&R--";
excel.SetSheetHeaderFooter(sheetName, headerFooter);

// SetSheetVisible
excel.SetSheetVisible(sheetName, True);
println("[success] SetSheetVisible");

// GetSheetVisible
visible := excel.GetSheetVisible(sheetName);
println("[success] GetSheetVisible = {}", visible);

// SetRowVisible
excel.SetRowVisible(sheetName, 2, false);
println("[success] SetRowVisible");

// GetRowVisble
[err, errmsg] := excel.GetRowVisble(sheetName, 2);
PrintInfo("GetRowVisble 2", err, errmsg);
[err, errmsg] := excel.GetRowVisble(sheetName, 3);
PrintInfo("GetRowVisble 3", err, errmsg);

// SetColVisible
excel.SetColVisible(sheetName, "A", false);
println("[success] SetRowVisible");

// GetColVisble
[err, errmsg] := excel.GetColVisble(sheetName, "A");
PrintInfo("GetColVisible", err, errmsg);

// SetPageMargins
margins := TOfficeObj('TMargins');
margins.Top := 0.5;
margins.Bottom := 0.6;
margins.Left := 0.7;
margins.Right := 0.8;
margins.Header := 0.9;
margins.Footer := 1.0;
excel.SetPageMargins(sheetName2, margins);
println("[success] SetPageMargins");

// GetPageMargins
margins := excel.GetPageMargins(sheetName2);
println("[success] GetPageMargins  top = {}, bottom = {}, left = {}, right = {}, header = {}, footer = {}",
		margins.Top, margins.Bottom, margins.Left, margins.Right,
		margins.Header, margins.Footer);

// MergeCell
excel.SetCellStyle(sheetName2, "A4", "A4", styleid1);
excel.MergeCell(sheetName2, "A4", "C4");
excel.MergeCell(sheetName2, "A5", "D7");
println("[success] MergeCell");

// UnMergeCell
excel.UnMergeCell(sheetName2, "A4", "C4");
println("[success] UnMergeCell");


// SetSheetViewOptions
sheetView := TOfficeObj('TSheetView');
sheetView.ShowGridLines := false;
sheetView.ShowRowColHeaders := false;
sheetView.View := 'pageBreakPreview';
sheetView.ZoomScale := 60;
excel.SetSheetViewOptions(sheetName, 0, sheetView);
println("[success] SetSheetViewOptions");

// GetSheetViewOptions
view := excel.GetSheetViewOptions(sheetName, 0);
println("[success] GetSheetViewOptions  ShowGridLines = {}, ShowRowColHeaders = {}, View = {}, ZoomScale = {}", view.ShowGridLines, view.ShowRowColHeaders, view.ZoomScale, view.View);

// SetPane
Pane := TOfficeObj('TPane');
Pane.XSplit := 6;
Pane.YSplit := 14;
Pane.ActivePane := 'bottomRight';
Pane.TopLeftCell := excel.CoordinatesToCellName(Pane.XSplit + 1, Pane.YSplit + 1)[1];
Pane.State := 'frozen';
excel.SetPane(sheetName, 0, Pane);
println("[success] SetPane");

// SetPageLayout
pageLayout := TOfficeObj('TPageLayout');
pageLayout.FitToWidth := 10;
pageLayout.FitToHeight := 10;
pageLayout.UseFirstPageNumber := true;
pageLayout.FirstPageNumber := 5;
pageLayout.Orientation := "portrait";
pageLayout.BlackAndWhite := true;
pageLayout.PaperSize := 11;
pageLayout.PageOrder := "overThenDown";
pageLayout.CellError := "blank";
pageLayout.CellComments := "asDisplayed";
excel.SetPageLayout(sheetName2, pageLayout);
println("[success] SetPageLayout");

// GetPageLayout
pageLayout := excel.GetPageLayout(sheetName2);
println("[success] GetPageLayout  FitToWidth = {}, PaperSize = {}", pageLayout.Value('FitToWidth'), pageLayout.Value('PaperSize'));

// SetDefaultSheet
excel.SetDefaultSheet(sheetName2);
println("[success] SetDefaultSheet");

// GetDefaultSheet
sheet := excel.GetDefaultSheet();
println("[success] GetDefaultSheet = {}", sheet);

// SetCellHyperLink
link := TOfficeObj('THyperLink');
link.LinkType := "location";
link.LinkUrl := sheetName $ "!A1";
excel.SetCellHyperLink(sheetName2, 'A1', link);
link := TOfficeObj('THyperLink');
link.LinkType := "external";
link.LinkUrl := "https://www.baidu.com";
link.Tooltip := "超链接悬浮提示";
excel.SetCellValue(sheetName2, 'A2', '超链接');
excel.SetCellHyperLink(sheetName2, 'A2', link);
println("[success] SetCellHyperLink");

// GetCellHyperLink
hyperlink := excel.GetCellHyperLink(sheetName2, 'A2');
println("[success] GetCellHyperLink  LinkType = {}, LinkUrl = {}, Tooltip = {}", hyperlink.LinkType, hyperlink.LinkUrl, hyperlink.Tooltip);

// SetSheetBackground
imagepath := "C:\\Users\\csh05\\Pictures\\1png.png";
ret := readfile(rwBinary(), "", imagepath, 0, 1024000, data);
if not ret then return echo "readfile error!";
Image := TOfficeObj('TPicture');
picture.Image := data;
excel.SetSheetBackground(sheetName, picture);
println("[success] SetSheetBackground");

// AddPicture
format := TOfficeObj('TPictureFormat');
format.BegColOff := 20000;
format.EndColOff := 20000;
excel.AddPicture(sheetName, 'A11', 'F33', picture, format);
excel.AddPicture(sheetName2, 'A8', 'D16', picture, format);
println("[success] AddPicture");

// DeletePicture
excel.DeletePicture(sheetName2, 'A1', 'D17');
println("[success] DeletePicture");

// TODO 可能有问题
// AddTable
table2 := array(("title1" :1, "title2": 2), ("title1": 3, "title2": 4));
excel.InsertTable(sheetName2, "A9", table2, true);
tablestyle := TOfficeObj('TTableStyle');
tablestyle.TableName := "测试表";
tablestyle.TableStyle := "TableStyleMedium2";
tablestyle.ShowFirstColumn := True;
tablestyle.ShowLastColumn := True;
tablestyle.ShowRowStripes := True;
tablestyle.ShowColumnStripes := True;
excel.AddTable(sheetName2, "A9", "B11", tablestyle, false);
println("[success] AddTable");

// InsertPageBreak
excel.InsertPageBreak(sheetName2, 10);
excel.InsertPageBreak(sheetName2, 15);
println("[success] InsertPageBreak");

// RemovePageBreak
excel.RemovePageBreak(sheetName2, 15);
println("[success] RemovePageBreak");

// AddShape
shape_format := TOfficeObj('TShapeFormat');
shape_format.BegColOff := 354252;
shape_format.EndColOff := 7653;
shape_format.BegRowOff := 55577;
shape_format.EndRowOff := 66675;
shape_type := "roundRect";
excel.AddShape(sheetName2, "A15", "C17", shape_type, shape_format);
println("[success] AddShape");

// SetCoreProps
core := TOfficeObj('TCoreProperty');
core.Title := "标题";
core.Subject := "主题";
core.Creator := "作者";
excel.SetCoreProps(core);

// GetCoreProps
core := excel.GetCoreProps();
println("[success] GetCoreProps  Title = {}, subject = {}, Creator = {}", core.Title, core.Subject, core.Creator);

// SetAppProps
appProps := TOfficeObj('TAppProperty');
appProps.Application := "Microsoft TX";
appProps.DocSecurity := false;
appProps.ScaleCrop := "true";
appProps.Company := "TS";
appProps.LinksUpToDate := "true";
appProps.HyperlinksChanged := "true";
appProps.Version := "1.000";
appProps.Manager := "这是主管";
//appProps.HyperlinkBase := "这是超链接基础";
excel.SetAppProps(appProps);
println("[success] SetAppProps");

// GetAppProps
app_props := excel.GetAppProps();
println("[success] GetAppProps Manager = {}, Company = {}, Application = {}", app_props.Manager, app_props.Company, app_props.Application);

// CopySheet
excel.CopySheet(sheetName2, 'copy');
println("[success] CopySheet");

// ProtectSheet
protect := TOfficeObj('TProtect');
protect.EditObjects := true;
protect.EditScenarios := true;
protect.FormatCells := true;
protect.FormatColumns := true;
protect.FormatRows := true;
protect.InsertColumns := true;
protect.InsertRows := true;
protect.InsertHyperlinks := true;
protect.DeleteColumns := true;
protect.DeleteRows := true;
protect.Sort := true;
protect.AutoFilter := true;
protect.PivotTables := true;
protect.SelectLockedCells := true;
protect.SelectUnLockedCells := true;
excel.ProtectSheet(sheetName2, protect);
println("[success] ProtectSheet");

// SetDefaultFont
font := tofficeobj('tfont');
font.Name := '黑体';
font.Size := 13;
font.ThemeColor := 1;
excel.SetDefaultFont(font);
println("[success] SetDefaultFont");

tfont := excel.GetDefaultFont();
println("[success] GetDefaultFont , name = {}, size = {}, themecolor = {}", tfont.Name, tfont.Size, tfont.ThemeColor);

calcpr := TOfficeObj('TCalcPr');
calcpr.CalcMode := "";
calcpr.RefMode := "R1C1";
calcpr.Iterate := 1;
calcpr.IterateCount := 300;
calcpr.IterateDelta := "3E-3";
calcpr.CalcOnSave := true;
calcpr.ConCurrentCalc := true;
calcpr.ConCurrentManualCount := 4;
calcpr.FullPrecision := 0;
excel.SetCalcOptions(calcpr);
println("[success] SetCalcOptions");

calc := excel.GetCalcOptions();
println("[success] GetCalcOptions, CalcMode = {}, RefMode = {}, Iterate = {},
	IterateCount = {}, IterateDelta = {}, CalcOnSave = {}, conCurrentCalc = {},
	conCurrentManualCount = {}, FullPrecision = {}", calc.CalcMode, calc.RefMode, calc.Iterate,
	calc.IterateCount, calc.IterateDelta, calc.CalcOnSave, calc.conCurrentCalc,
	calc.conCurrentManualCount, calc.FullPrecision);

excel.SetRowOutlineLevel(sheetName2, 1, 1);
excel.SetRowOutlineLevel(sheetName2, 2, 1);
println("[success] SetRowOutlineLevel");

level1 := excel.GetRowOutlineLevel(sheetName2, 1);
level2 := excel.GetRowOutlineLevel(sheetName2, 3);

println("[success] GetRowOutlineLevel, level1 = {}, level2 = {}", level1, level2);

excel.SetColOutlineLevel(sheetName2, 'A', 1);
excel.SetColOutlineLevel(sheetName2, 'B', 2);
excel.SetColOutlineLevel(sheetName2, 'C', 1);
println("[success] SetColOutlineLevel");

level1 := excel.GetColOutlineLevel(sheetName2, 'A');
level2 := excel.GetColOutlineLevel(sheetName2, 'B');
println("[success] GetColOutlineLevel, level1 = {}, level2 = {}", level1, level2);

[err, errmsg] := excel.saveas("", "d:\\temp\\test.xlsx");
println("saveas : {}", err);

// ============================================================
Function PrintInfo(msg, err, errinfo);
Begin
	if err then debugreturn println("{} : errorno = {}, errorinfo = {}!", msg, err, errinfo);
	println("[success] {} = {}", msg, errinfo);
End
