code update
This commit is contained in:
177
main.go
177
main.go
@@ -3,28 +3,98 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/iancoleman/orderedmap"
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
// Initial concept from https://stackoverflow.com/q/68621039
|
||||
|
||||
func main() {
|
||||
jsonFile := "test.json"
|
||||
parentNode := "input"
|
||||
s, err := ioutil.ReadFile("test.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
sheetName := "Sheet2"
|
||||
outputFilename := "test.xlsx"
|
||||
|
||||
var xlsx *excelize.File
|
||||
var s []byte
|
||||
|
||||
var sheetIndex int
|
||||
var err error
|
||||
|
||||
var cell string
|
||||
var row, column int
|
||||
|
||||
// TODO - truncate sheetName to the maximum 31 characters
|
||||
|
||||
// Check if xlsx file exists already, and if it does then open and append data
|
||||
if fileExists(outputFilename) {
|
||||
fmt.Printf("Output spreadsheet '%s' already exists.\n", outputFilename)
|
||||
xlsx, err = excelize.OpenFile(outputFilename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Since we have an existing workbook, check if the sheet we want to write to already exists
|
||||
sheetFound := false
|
||||
for index, name := range xlsx.GetSheetMap() {
|
||||
if name == sheetName {
|
||||
fmt.Printf("Found worksheet '%s' at index '%d'\n", sheetName, index)
|
||||
sheetFound = true
|
||||
}
|
||||
}
|
||||
if !sheetFound {
|
||||
// Create the sheet
|
||||
fmt.Printf("Creating worksheet '%s'\n", sheetName)
|
||||
sheetIndex, err = xlsx.NewSheet(sheetName)
|
||||
if err != nil {
|
||||
fmt.Printf("Error creating worksheet '%s' : %s\n", sheetName, err)
|
||||
}
|
||||
// Set active worksheet
|
||||
fmt.Printf("Setting active sheet to index %d", sheetIndex)
|
||||
xlsx.SetActiveSheet(sheetIndex)
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Printf("Creating output spreadsheet '%s'\n", outputFilename)
|
||||
// Create the file
|
||||
xlsx = excelize.NewFile()
|
||||
|
||||
// Rename the default Sheet1 to this worksheet name
|
||||
if sheetName != "Sheet1" {
|
||||
fmt.Printf("Renaming default worksheet to '%s'\n", sheetName)
|
||||
err = xlsx.SetSheetName("Sheet1", sheetName)
|
||||
if err != nil {
|
||||
fmt.Printf("Error setting sheet name to '%s': %s\n", sheetName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TestUnmarshalJSON()
|
||||
// Read the json input file
|
||||
if fileExists(jsonFile) {
|
||||
s, err = os.ReadFile(jsonFile)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Input JSON file '%s' does not exist.\n", jsonFile)
|
||||
}
|
||||
|
||||
// Read the json into an orderedmap to preserve the ordering of json structure
|
||||
o := orderedmap.New()
|
||||
err = json.Unmarshal([]byte(s), &o)
|
||||
if err != nil {
|
||||
fmt.Printf("JSON Unmarshal error", err)
|
||||
fmt.Printf("JSON Unmarshal error %s\n", err)
|
||||
}
|
||||
|
||||
// Assume that our content is within the first top-level key
|
||||
topLevel := o.Keys()
|
||||
fmt.Printf("topLevel: %v\n", topLevel)
|
||||
parentNode = topLevel[0]
|
||||
|
||||
// Get a reference to the top level node we specified earlier
|
||||
// TODO - validate this key exists
|
||||
vislice, ok := o.Get(parentNode)
|
||||
@@ -38,22 +108,63 @@ func main() {
|
||||
columnNames := columnMap.Keys()
|
||||
fmt.Printf("Creating excel workbook with following headings : '%v'\n", columnNames)
|
||||
|
||||
// Add the header row
|
||||
row = 1
|
||||
column = 1
|
||||
for i := 0; i < len(columnNames); i++ {
|
||||
cell, _ = excelize.CoordinatesToCellName(column, row)
|
||||
fmt.Printf("Setting cell %s to value %s\n", cell, columnNames[i])
|
||||
xlsx.SetCellValue(sheetName, cell, columnNames[i])
|
||||
column++
|
||||
}
|
||||
|
||||
// Set starting row for data
|
||||
row = 2
|
||||
|
||||
fmt.Printf("Adding %d rows of data to spreadsheet.\n", len(vslice))
|
||||
|
||||
// Iterate the whole slice to get the data and add to the worksheet
|
||||
for i, v := range vslice {
|
||||
// Print the contents each slice
|
||||
//fmt.Printf("'%d' : '%v'\n", i, v)
|
||||
|
||||
fmt.Printf("Slice element %d\n", i)
|
||||
//fmt.Printf("Slice element %d\n", i)
|
||||
_ = i
|
||||
|
||||
// Each iteration should start back at column 1
|
||||
column = 1
|
||||
|
||||
// Print each key-value pair contained in this slice
|
||||
vmap := v.(orderedmap.OrderedMap)
|
||||
k := vmap.Keys()
|
||||
|
||||
for j := range k {
|
||||
//a = string(asciiValue)
|
||||
//cell = a + strconv.Itoa(2+i)
|
||||
cell, _ = excelize.CoordinatesToCellName(column, row)
|
||||
|
||||
e, _ := vmap.Get(k[j])
|
||||
fmt.Printf("'%s' : '%v'\n", k[j], e)
|
||||
//fmt.Printf("Setting cell %s to value %v\n", cell, e)
|
||||
xlsx.SetCellValue(sheetName, cell, e)
|
||||
|
||||
// Move to the next column
|
||||
//asciiValue++
|
||||
column++
|
||||
}
|
||||
//fmt.Printf("k: %v\n", k)
|
||||
|
||||
// Move to next row
|
||||
row++
|
||||
}
|
||||
|
||||
// Close off the file
|
||||
if err := xlsx.SaveAs(outputFilename); err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Excel file '%s' generated sucessfully.\n", outputFilename)
|
||||
|
||||
/*
|
||||
//Test https://gitlab.com/c0b/go-ordered-json
|
||||
var om *ordered.OrderedMap = ordered.NewOrderedMap()
|
||||
@@ -71,50 +182,14 @@ func main() {
|
||||
fmt.Printf("%-12s: %v\n", pair.Key, pair.Value)
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
var data interface{}
|
||||
json.Unmarshal(file, &data) // reading all json data
|
||||
//fmt.Println(data)
|
||||
t, ok := data.([]interface{}) // assertion Interface
|
||||
_ = ok
|
||||
//fmt.Printf("%[1]v %[1]T\n", t) //map[string]interface {}
|
||||
|
||||
myMap := t[0] // aim here to get APP, Company and Category which will be the column name of Excel sheet
|
||||
fmt.Printf("myMap: %v\n", myMap)
|
||||
columnData, _ := myMap.(map[string]interface{}) // extract the underlying concrete data from interface
|
||||
fmt.Printf("columnData: %v\n", columnData)
|
||||
keys := make([]string, 0, len(columnData)) // creating and initializing slice to store column
|
||||
|
||||
for k := range columnData {
|
||||
fmt.Printf("%[1]v %[1]T\n", k)
|
||||
keys = append(keys, k)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
xlsx := excelize.NewFile()
|
||||
sheetName := "Sheet1"
|
||||
xlsx.SetSheetName(xlsx.GetSheetName(1), sheetName)
|
||||
c := 'A'
|
||||
asciiValue := int(c)
|
||||
var a string
|
||||
|
||||
for i := 0; i < len(keys); i++ {
|
||||
a = string(asciiValue)
|
||||
xlsx.SetCellValue(sheetName, a+"1", keys[i])
|
||||
asciiValue++
|
||||
}
|
||||
|
||||
err = xlsx.SaveAs("./Onkar.xlsx")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Excel file generated sucessfully")
|
||||
*/
|
||||
func fileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
|
||||
func TestUnmarshalJSON() {
|
||||
|
Reference in New Issue
Block a user