Skip to content

Commit

Permalink
feat(main.go, csfutil.go, excel.go): Implement new file and excel imp…
Browse files Browse the repository at this point in the history
…ort / export logic
  • Loading branch information
Zhwt committed May 1, 2022
1 parent 07b33a9 commit 0905bbe
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 11 deletions.
89 changes: 78 additions & 11 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/Zhwt/csfutil"
"github.com/Zhwt/csfutil/utils"
"os"
"strconv"
)

var usageMessages = map[string]string{
Expand All @@ -15,15 +16,19 @@ Exports all CSF LabelValue items inside the given CSF file into a spreadsheet. T
<CSF Label Name> <CSF Value Value> <CSF Value ExtraValue>
This file can be imported into a CSF file later.`,
"import": `Usage: csfutil import <filename.csf> <input.xlsx>
"import": `Usage: csfutil import <input.xlsx> <filename.csf>
Import all CSF LabelValue items inside the spread sheet into the CSF file. Existing items will be overwritten and missing items will be created.`,
Import all CSF LabelValue items inside the spread sheet into the CSF file. Existing items will be overwritten and missing items will be created. The file must have the following structure:
<CSF Label Name> <CSF Value Value> <CSF Value ExtraValue>
Otherwise the`,
"merge": `Usage: csfutil merge <source.csf> <destination.csf>
Merges all CSF LabelValue items inside the source file into the destination file. Only existing items will be overwritten and missing items won't' be created.`,
"new": `Usage: csfutil new <filename.csf>
"new": `Usage: csfutil new <filename.csf> [language code]
Create a empty csf file.`,
Create a empty Version 3 csf file. Valid language code can be 0~9, otherwise it will be recognized as "Unknown".`,
"help": `csfutil is a tool for manipulating CSF files.
Usage:
Expand All @@ -35,7 +40,7 @@ The commands are:
export convert a CSF file to a spreadsheet
import merge items from a spreadsheet into a CSF file
merge merge one CSF file into another
new create empty CSF file
new create empty Version 3 CSF file
Use "csfutil help <command>" for more information about a command.`,
}
Expand All @@ -49,14 +54,18 @@ func help(cmd string) {
}
}

func printError(err error) {
fmt.Println("Error:", err)
}

func merge(src, dst string) error {
srcFile, err := csfutil.Open(src)
if err != nil {
return fmt.Errorf("%s: %w", src, err)
}
dstFile, err := csfutil.Open(dst)
if err != nil {
return fmt.Errorf("%s: %w", src, err)
return fmt.Errorf("%s: %w", dst, err)
}

for _, s := range dstFile.Order {
Expand All @@ -81,21 +90,79 @@ func main() {
} else {
switch os.Args[1] {
case "export":
fmt.Println("not implemented")
if argCount == 3 || argCount == 4 {
input := os.Args[2]
output := "output.xlsx"
if argCount == 4 {
output = os.Args[3]
}

csf, err := csfutil.Open(input)
if err != nil {
printError(err)
return
}

err = csfutil.ExportExcel(csf, output)
if err != nil {
printError(err)
return
}
} else {
fmt.Println("Incorrect argument count, want 3 or 4, got", argCount)
help(os.Args[1])
}
case "import":
fmt.Println("not implemented")
if argCount == 4 {
input := os.Args[2]
output := os.Args[3]
err := csfutil.ImportExcel(input, output)
if err != nil {
printError(err)
return
}
} else {
fmt.Println("Incorrect argument count, want 4, got", argCount)
help(os.Args[1])
}
case "merge":
if argCount == 4 {
err := merge(os.Args[2], os.Args[3])
if err != nil {
fmt.Println("Error:", err)
printError(err)
return
}
} else {
fmt.Println("Incorrect argument count, want 4, got", argCount)
help("merge")
help(os.Args[1])
}
case "new":
fmt.Println("not implemented")
if argCount == 3 || argCount == 4 {
output := os.Args[2]
language := 0
var err error
if argCount == 4 {
language, err = strconv.Atoi(os.Args[3])
if err != nil {
printError(err)
return
}
}

csf := csfutil.New(output, 3, 0, uint(language))
if err != nil {
printError(err)
return
}

if err = csf.Save(); err != nil {
printError(err)
return
}
} else {
fmt.Println("Incorrect argument count, want 3 or 4, got", argCount)
help(os.Args[1])
}
case "help":
if argCount > 2 {
help(os.Args[2])
Expand Down
14 changes: 14 additions & 0 deletions csfutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,17 @@ func MustOpen(name string) *CSFUtil {

return u
}

func New(name string, version, unused, language uint) *CSFUtil {
return &CSFUtil{
filename: name,
Version: version,
NumLabels: 0,
NumStrings: 0,
Unused: unused,
Language: language,
Values: map[string]LabelValue{},
Categories: map[string][]string{},
Order: []string{},
}
}
76 changes: 76 additions & 0 deletions excel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package csfutil

import (
"fmt"
"github.com/xuri/excelize/v2"
"strconv"
)

const (
sheet = "Sheet1"
labelColumn = "A"
valueColumn = "B"
extraValueColumn = "C"
)

func ExportExcel(csf *CSFUtil, output string) error {
f := excelize.NewFile()
defer f.Close()

for i := 0; i < len(csf.Order); i++ {
row := strconv.Itoa(i + 1)

if err := f.SetCellValue(sheet, labelColumn+row, csf.Values[csf.Order[i]].Label.ValueString()); err != nil {
return err
}

if err := f.SetCellValue(sheet, valueColumn+row, csf.Values[csf.Order[i]].Value.ValueString()); err != nil {
return err
}

if csf.Values[csf.Order[i]].Value.HaveExtra {
if err := f.SetCellValue(sheet, extraValueColumn+row, csf.Values[csf.Order[i]].Value.ExtraValueString()); err != nil {
return err
}
}
}

return f.SaveAs(output)
}

func ImportExcel(input, output string) error {
f, err := excelize.OpenFile(input)
if err != nil {
return err
}
defer f.Close()

csf, err := Open(output)
if err != nil {
return err
}

rows, err := f.GetRows(sheet)
if err != nil {
return err
}

if len(rows) < 1 {
return fmt.Errorf("no data to read")
}

for i, row := range rows {
switch len(row) {
case 1:
csf.WriteLabelValue(NewLabelValue(row[0], ""), false)
case 2:
csf.WriteLabelValue(NewLabelValue(row[0], row[1]), false)
case 3:
csf.WriteLabelValue(NewLabelValue(row[0], row[1], row[2]), false)
default:
return fmt.Errorf("wrong column count, want 2~3, got %d, at row: %d", len(row), i)
}
}

return csf.Save()
}

0 comments on commit 0905bbe

Please sign in to comment.