Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
d5a803644b | |||
bfa2afbb6c | |||
24560ee8bf | |||
2a89008a54 | |||
c91d38f96c | |||
482e5deb6e | |||
590d3e3407 | |||
d872cb8517 | |||
4d021813f6 | |||
55f3196c4b | |||
7e3e2a2185 | |||
4bffb9cbfc |
15
.drone.sh
Normal file
15
.drone.sh
Normal file
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
|
||||
# disable CGO for cross-compiling
|
||||
export CGO_ENABLED=0
|
||||
|
||||
export now=$(TZ=Australia/Sydney date '+%Y%m%d-%H%M%S')
|
||||
echo $now
|
||||
#pwd
|
||||
#ls -lah ~
|
||||
#go env
|
||||
echo "build commences"
|
||||
go build -ldflags "-X main.sha1ver=`git rev-parse HEAD` -X main.buildTime=$now" -o json2excel
|
||||
echo "build complete"
|
||||
sha256sum json2excel > json2excel_checksum.txt
|
||||
ls -lah
|
30
.drone.yml
Normal file
30
.drone.yml
Normal file
@@ -0,0 +1,30 @@
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: default
|
||||
|
||||
# Docs at https://docs.drone.io/pipeline/exec/overview/
|
||||
# Also see https://github.com/harness/drone-cli/blob/master/.drone.yml
|
||||
|
||||
steps:
|
||||
- name: build
|
||||
image: golang
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
commands:
|
||||
- sh ./.drone.sh
|
||||
|
||||
- name: dell-sftp-deploy
|
||||
image: hypervtechnics/drone-sftp
|
||||
settings:
|
||||
host: deft.dell.com
|
||||
username:
|
||||
from_secret: DELLFTP_USER
|
||||
password:
|
||||
from_secret: DELLFTP_PASS
|
||||
port: 22
|
||||
source: ./
|
||||
filter: json2excel*
|
||||
clean: false
|
||||
target: /
|
||||
overwrite: true
|
||||
verbose: true
|
6
.gitignore
vendored
6
.gitignore
vendored
@@ -5,4 +5,8 @@
|
||||
json2excel
|
||||
|
||||
# Ignore test data
|
||||
*.json
|
||||
*.json
|
||||
|
||||
# Ignore Mac DS_Store files
|
||||
.DS_Store
|
||||
**/.DS_Store
|
11
README.md
11
README.md
@@ -14,3 +14,14 @@ It expects that the json input is formatted as an object containing an array of
|
||||
| freezeTopRow | true | Freezes the first row of the Excel worksheet |
|
||||
| autofilter | true | Sets the auto filter on the first row |
|
||||
| autowidth | true | Automatically set the column width to fit contents |
|
||||
| overwriteFile | false | Overwrite any existing output file instead of modifying in-place |
|
||||
|
||||
## Advanced configuration
|
||||
|
||||
Advanced settings can be provided via a top level json key named "config". Here is a table of options that can be set via this config key.
|
||||
|
||||
| Key | Example Value | Description |
|
||||
|---------------|---------------|---------------------------|
|
||||
| keyOrder | "Column3,Column1,Column2"| Comma separated list of column names in the desired order |
|
||||
| overwriteFile | true | Boolean indicating whether output file should be overwritten if it already exists |
|
||||
| parentNode | "results" | Specify an alternate starting key for the spreadsheet data than just the first non-config key. Useful with json structures with multiple top-level keys |
|
BIN
cmd/.DS_Store
vendored
BIN
cmd/.DS_Store
vendored
Binary file not shown.
277
cmd/main/main.go
277
cmd/main/main.go
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
@@ -15,9 +17,17 @@ import (
|
||||
|
||||
// Initial concept from https://stackoverflow.com/q/68621039
|
||||
|
||||
type Config struct {
|
||||
keyOrder map[int]string
|
||||
parentOverride string
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
func main() {
|
||||
//jsonFile := "test.json"
|
||||
parentNode := "input"
|
||||
configNode := "config"
|
||||
//worksheetName := "Sheet2"
|
||||
//outputFilename := "test.xlsx"
|
||||
|
||||
@@ -54,6 +64,152 @@ func main() {
|
||||
// Truncate worksheetName to the maximum 31 characters
|
||||
worksheetName = TruncateString(worksheetName, 31)
|
||||
|
||||
// Read the json input file
|
||||
if fileExists(inputJson) {
|
||||
s, err = os.ReadFile(inputJson)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Input JSON file '%s' does not exist.\n", inputJson)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Unmarshal the json into an orderedmap to preserve the ordering of json structure
|
||||
o := orderedmap.New()
|
||||
err = json.Unmarshal([]byte(s), &o)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("JSON Unmarshal error %s\n", err)
|
||||
panic(error)
|
||||
}
|
||||
|
||||
// Assume that our content is within the first top-level key
|
||||
topLevel := o.Keys()
|
||||
fmt.Printf("Found %d top-level keys in json data\n", len(topLevel))
|
||||
for i, key := range topLevel {
|
||||
fmt.Printf("[%d] : %s\n", i, key)
|
||||
}
|
||||
|
||||
// Check for config embedded in json
|
||||
if strings.EqualFold(topLevel[0], configNode) && len(topLevel) > 1 {
|
||||
fmt.Printf("Found configNode as toplevel json key, setting parentNode as '%s'\n", topLevel[1])
|
||||
parentNode = topLevel[1]
|
||||
|
||||
config.keyOrder = make(map[int]string)
|
||||
|
||||
// Get a reference to the top level node we specified earlier
|
||||
configInterface, ok := o.Get(configNode)
|
||||
if !ok {
|
||||
fmt.Printf("Missing key for multitype array when reading embedded config")
|
||||
}
|
||||
|
||||
// Get an interface that we can work with to access the sub elements
|
||||
// This doesn't seem necessary for some reason - maybe because there's only one level of depth to the configNode
|
||||
//configSlice := configInterface
|
||||
//fmt.Printf("%v\n", configSlice)
|
||||
|
||||
// Get the keys for the first element so we know what config options have been specified
|
||||
configMap := configInterface.(orderedmap.OrderedMap)
|
||||
configKeys := configMap.Keys()
|
||||
|
||||
// Parse each key into our config struct
|
||||
for _, key := range configKeys {
|
||||
if strings.EqualFold(key, "keyOrder") {
|
||||
fmt.Printf("Found config element for keyOrder\n")
|
||||
e, _ := configMap.Get(key)
|
||||
for i, e := range strings.Split(e.(string), ",") {
|
||||
config.keyOrder[i] = e
|
||||
}
|
||||
fmt.Printf("Column order is now : '%v'\n", config.keyOrder)
|
||||
} else if strings.EqualFold(key, "overwriteFile") {
|
||||
fmt.Printf("Found config element for overwriting output file\n")
|
||||
e, _ := configMap.Get(key)
|
||||
overwriteFile = e.(bool)
|
||||
} else if strings.EqualFold(key, "parentNode") {
|
||||
fmt.Printf("Found config element for forcing parent key for spreadsheet data\n")
|
||||
e, _ := configMap.Get(key)
|
||||
config.parentOverride = e.(string)
|
||||
}
|
||||
}
|
||||
} else if strings.EqualFold(topLevel[0], configNode) {
|
||||
error := "Only found config in first level of json keys"
|
||||
panic(error)
|
||||
} else {
|
||||
fmt.Printf("Detected toplevel json key as: '%s'\n", topLevel[0])
|
||||
parentNode = topLevel[0]
|
||||
}
|
||||
|
||||
if config.parentOverride != "" {
|
||||
fmt.Printf("Overriding parent node to '%s'\n", config.parentOverride)
|
||||
parentNode = config.parentOverride
|
||||
}
|
||||
|
||||
// Get a reference to the top level node we specified earlier
|
||||
vislice, ok := o.Get(parentNode)
|
||||
if !ok {
|
||||
fmt.Printf("Missing key for multitype array")
|
||||
}
|
||||
|
||||
// Get an interface that we can work with to access the sub elements
|
||||
vslice := vislice.([]interface{})
|
||||
|
||||
if len(vslice) < 1 {
|
||||
// There was no data but lets just log that and close off the empty workbook
|
||||
fmt.Printf("No data found contained in top-level json key '%s', no work to do.\n", parentNode)
|
||||
|
||||
// Create new file
|
||||
xlsx = createWorkbook(worksheetName, outputFilename)
|
||||
|
||||
// Run code to add column names to the first row of the workbook based off the keyOrder, if defined
|
||||
if len(config.keyOrder) > 0 {
|
||||
var columnHeadings []string
|
||||
for _, value := range config.keyOrder {
|
||||
columnHeadings = append(columnHeadings, value)
|
||||
}
|
||||
createHeadingRow(xlsx, worksheetName, columnHeadings)
|
||||
|
||||
headerStyle, err := xlsx.NewStyle(&excelize.Style{
|
||||
Font: &excelize.Font{
|
||||
Bold: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Error generating header style : '%s'\n", err)
|
||||
} else {
|
||||
// Set the style if there was no error generating it
|
||||
if boldTopRow {
|
||||
err = xlsx.SetRowStyle(worksheetName, 1, 1, headerStyle)
|
||||
if err != nil {
|
||||
fmt.Printf("Error setting header style : '%s'\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if autoWidth {
|
||||
err = SetColAutoWidth(xlsx, worksheetName)
|
||||
if err != nil {
|
||||
fmt.Printf("Error setting auto width : '%s'\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close off the file
|
||||
if err := xlsx.SaveAs(outputFilename); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Check that the first element is what we expected
|
||||
if _, ok := vslice[0].(orderedmap.OrderedMap); !ok {
|
||||
error := fmt.Sprintf("Type of first vslice element is not an ordered map. It appears to be '%v'\n", reflect.TypeOf(vslice[0]))
|
||||
panic(error)
|
||||
}
|
||||
// Get the keys for the first element so we know what the column names will be
|
||||
columnMap := vslice[0].(orderedmap.OrderedMap)
|
||||
//fmt.Printf("First vslice element is an ordered map\n")
|
||||
columnNames := columnMap.Keys()
|
||||
|
||||
// Check if xlsx file exists already, and if it does then open and append data
|
||||
if fileExists(outputFilename) {
|
||||
if overwriteFile {
|
||||
@@ -74,41 +230,6 @@ func main() {
|
||||
xlsx = createWorkbook(worksheetName, outputFilename)
|
||||
}
|
||||
|
||||
// Read the json input file
|
||||
if fileExists(inputJson) {
|
||||
s, err = os.ReadFile(inputJson)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Input JSON file '%s' does not exist.\n", inputJson)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Unmarshal 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 %s\n", err)
|
||||
}
|
||||
|
||||
// Assume that our content is within the first top-level key
|
||||
topLevel := o.Keys()
|
||||
fmt.Printf("Detected toplevel json key as: '%s'\n", topLevel[0])
|
||||
parentNode = topLevel[0]
|
||||
|
||||
// Get a reference to the top level node we specified earlier
|
||||
vislice, ok := o.Get(parentNode)
|
||||
if !ok {
|
||||
fmt.Printf("Missing key for multitype array")
|
||||
}
|
||||
// Get an interface that we can work with to access the sub elements
|
||||
vslice := vislice.([]interface{})
|
||||
|
||||
// Get the keys for the first element so we know what the column names will be
|
||||
columnMap := vslice[0].(orderedmap.OrderedMap)
|
||||
columnNames := columnMap.Keys()
|
||||
|
||||
// Run code to add column names to the first row of the workbook
|
||||
createHeadingRow(xlsx, worksheetName, columnNames)
|
||||
|
||||
@@ -122,13 +243,13 @@ func main() {
|
||||
})
|
||||
if err2 != nil {
|
||||
fmt.Printf("Error generating header style : '%s'\n", err2)
|
||||
}
|
||||
|
||||
// Set the style
|
||||
if boldTopRow {
|
||||
err = xlsx.SetRowStyle(worksheetName, 1, 1, headerStyle)
|
||||
if err != nil {
|
||||
fmt.Printf("Error setting header style : '%s'\n", err)
|
||||
} else {
|
||||
// Set the style if there was no error generating it
|
||||
if boldTopRow {
|
||||
err = xlsx.SetRowStyle(worksheetName, 1, 1, headerStyle)
|
||||
if err != nil {
|
||||
fmt.Printf("Error setting header style : '%s'\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,23 +299,37 @@ func main() {
|
||||
// Each iteration should start back at column 1
|
||||
column = 1
|
||||
|
||||
// Print each key-value pair contained in this slice
|
||||
// Get the key-value pairs 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)
|
||||
if len(config.keyOrder) > 0 {
|
||||
// If we have a specified order for the values then use that
|
||||
|
||||
e, _ := vmap.Get(k[j])
|
||||
//fmt.Printf("Setting cell %s to value %v\n", cell, e)
|
||||
xlsx.SetCellValue(worksheetName, cell, e)
|
||||
for j := 0; j < len(config.keyOrder); j++ {
|
||||
cell, _ = excelize.CoordinatesToCellName(column, row)
|
||||
|
||||
// Move to the next column
|
||||
//asciiValue++
|
||||
column++
|
||||
e, _ := vmap.Get(config.keyOrder[j])
|
||||
//fmt.Printf("Setting cell %s to value %v\n", cell, e)
|
||||
xlsx.SetCellValue(worksheetName, cell, e)
|
||||
|
||||
// Move to the next column
|
||||
column++
|
||||
}
|
||||
} else {
|
||||
// Otherwise use the order the json was in
|
||||
for j := range k {
|
||||
cell, _ = excelize.CoordinatesToCellName(column, row)
|
||||
|
||||
e, _ := vmap.Get(k[j])
|
||||
//fmt.Printf("Setting cell %s to value %v\n", cell, e)
|
||||
xlsx.SetCellValue(worksheetName, cell, e)
|
||||
|
||||
// Move to the next column
|
||||
column++
|
||||
}
|
||||
}
|
||||
|
||||
//fmt.Printf("k: %v\n", k)
|
||||
|
||||
// Move to next row
|
||||
@@ -314,19 +449,37 @@ func modifyWorkbook(worksheetName string, outputFilename string) *excelize.File
|
||||
}
|
||||
|
||||
func createHeadingRow(xlsx *excelize.File, worksheetName string, columnNames []string) {
|
||||
fmt.Printf("Creating excel workbook with following headings : '%v'\n", columnNames)
|
||||
var cell string
|
||||
row := 1
|
||||
column := 1
|
||||
|
||||
// Add the header row
|
||||
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(worksheetName, cell, columnNames[i])
|
||||
//xlsx.SetCellStyle(worksheetName, cell, cell, headerStyle)
|
||||
column++
|
||||
if len(config.keyOrder) > 0 {
|
||||
fmt.Printf("Creating excel workbook with heading order as per config key-order\n")
|
||||
// Check that the number of specified columns matches input data
|
||||
if len(config.keyOrder) != len(columnNames) {
|
||||
error := fmt.Sprintf("Column order specified in json key-order but mismatch found in json data. %d specifed columns does not match %d found columns.", len(config.keyOrder), len(columnNames))
|
||||
panic(error)
|
||||
}
|
||||
|
||||
// Iterate the map and add the columns as per that order
|
||||
for i := 0; i < len(config.keyOrder); i++ {
|
||||
cell, _ = excelize.CoordinatesToCellName(column, row)
|
||||
fmt.Printf("Setting cell %s to value %s at index %d\n", cell, config.keyOrder[i], i)
|
||||
xlsx.SetCellValue(worksheetName, cell, config.keyOrder[i])
|
||||
column++
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("Creating excel workbook with following headings : '%v'\n", columnNames)
|
||||
// Add the header row
|
||||
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(worksheetName, cell, columnNames[i])
|
||||
//xlsx.SetCellStyle(worksheetName, cell, cell, headerStyle)
|
||||
column++
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Taken from https://github.com/qax-os/excelize/issues/92#issuecomment-821578446
|
||||
@@ -344,7 +497,7 @@ func SetColAutoWidth(xlsx *excelize.File, sheetName string) error {
|
||||
largestWidth = cellWidth
|
||||
}
|
||||
}
|
||||
fmt.Printf("SetColAutoWidth calculated largest width is '%d'\n", largestWidth)
|
||||
fmt.Printf("SetColAutoWidth calculated largest width for column index '%d' is '%d'\n", idx, largestWidth)
|
||||
name, err := excelize.ColumnNumberToName(idx + 1)
|
||||
if err != nil {
|
||||
return err
|
||||
|
BIN
internal/.DS_Store
vendored
BIN
internal/.DS_Store
vendored
Binary file not shown.
Reference in New Issue
Block a user