当前位置: 首页>>代码示例>>Golang>>正文


Golang Table.AddRow方法代码示例

本文整理汇总了Golang中table.Table.AddRow方法的典型用法代码示例。如果您正苦于以下问题:Golang Table.AddRow方法的具体用法?Golang Table.AddRow怎么用?Golang Table.AddRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在table.Table的用法示例。


在下文中一共展示了Table.AddRow方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Generate

// Generate reports to output directory.
func (report *Report) Generate(outputDir string) error {
	var buf bytes.Buffer
	var filename string

	switch report.period {
	case ReportPeriodInfinite:
		fmt.Fprintf(&buf, "All transactions\n\n")
		filename = filepath.Join(outputDir, "all.txt")
	case ReportPeriodYearly:
		fmt.Fprintf(&buf, "%d\n\n", report.from.Year())
		filename = filepath.Join(outputDir, fmt.Sprintf("%d.txt", report.from.Year()))
	case ReportPeriodQuarterly:
		quarterName := fmt.Sprintf("Q%d", (report.from.Month()-1)/3+1)
		fmt.Fprintf(&buf, "%s %d\n\n", quarterName, report.from.Year())
		filename = filepath.Join(outputDir, fmt.Sprintf("%d-%s.txt", report.from.Year(), quarterName))
	case ReportPeriodMonthly:
		fmt.Fprintf(&buf, "%s %d\n\n", report.from.Month(), report.from.Year())
		filename = filepath.Join(outputDir, fmt.Sprintf("%d-%02d.txt", report.from.Year(), report.from.Month()))
	default:
		return fmt.Errorf("unsupported report period %d", report.period)
	}

	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer file.Close()

	var accounts []*Account
	for _, account := range report.accounts {
		accounts = append(accounts, account)
	}
	sort.Sort(sortByAccountName(accounts))

	// Account summary
	t := new(table.Table)
	t.SetTitles(table.Row{
		{Content: "account"},
		{Content: "amount"},
		{Content: "cumulative"},
		{Content: "delta"},
	})

	for _, account := range accounts {
		cumulativeStr := balanceToString(account.CumulativeBalance())
		deltaStr := fmt.Sprintf("%+.2f", report.AccountDelta(account.name))

		if cumulativeStr != "-" || deltaStr != "+0.00" {
			t.AddRow(table.Row{
				{Content: account.name.Leaf(), PadLeft: uint(indentAmount * account.name.Depth())},
				{Content: balanceToString(account.FlatBalance()), Align: table.AlignRight},
				{Content: cumulativeStr, Align: table.AlignRight},
				{Content: deltaStr, Align: table.AlignRight},
			})
		}
	}
	buf.Write(t.RenderText())

	// Transaction log
	fmt.Fprintf(&buf, "\nTransactions\n\n")

	t = new(table.Table)
	t.SetTitles(table.Row{
		{Content: "date"},
		{Content: "account"},
		{Content: "debit"},
		{Content: "credit"},
	})

	var prevDate time.Time
	for _, tr := range report.transactions {
		var dateStr string
		if tr.date != prevDate {
			dateStr = tr.date.Format(transactionDateFormat)
		}
		prevDate = tr.date

		t.AddRow(table.Row{
			{Content: dateStr},
			{Content: string(tr.accounts[Dr])},
			{Content: fmt.Sprintf("%.2f", tr.amount), Align: table.AlignRight},
			{Content: ""},
		})
		t.AddRow(table.Row{
			{Content: ""},
			{Content: string(tr.accounts[Cr])},
			{Content: ""},
			{Content: fmt.Sprintf("%.2f", tr.amount), Align: table.AlignRight},
		})
	}
	buf.Write(t.RenderText())

	fmt.Fprintf(file, "%s", buf.Bytes())
	return nil
}
开发者ID:johan-bolmsjo,项目名称:accounting,代码行数:96,代码来源:report.go


注:本文中的table.Table.AddRow方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。