Skip to content

Commit

Permalink
Merge pull request #3 from guanwee-loo/master
Browse files Browse the repository at this point in the history
Ignore empty rows or rows with non printable characters or whitespaces
  • Loading branch information
alabuel authored Dec 27, 2022
2 parents 936c423 + 999338a commit 9b053a3
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions config/data_source_configuration_workbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"github.com/360EntSecGroup-Skylar/excelize/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -269,6 +270,9 @@ func excelToCSV(args *ConfigurationWorkbook) (string, error) {
return "", fmt.Errorf("worksheet \"%s\" does not have data", args.sheet_name)
}

// delete empty rows or row containing non printable characters including white spaces
rows = delete_empty_row(rows)

// get the number of columns
row_len := len(rows[0])

Expand Down Expand Up @@ -651,3 +655,34 @@ func createDefaultMapping(items []string, csv []map[string]string, configuration

return mapping, nil
}

// Remove row that contains empty or row with non printer characters and invisible characters (white spaces)
func delete_empty_row(s [][]string) [][]string {
var r [][]string
for _, str := range s {
if len(str) != 0 {
if is_printable(str) {
r = append(r, str)
}
}
}
return r
}

// Check the string array for only printable characters excluding space
func is_printable(s []string) bool {
for _, str := range s {
str := strings.TrimSpace(str)
str = strings.Map(func(r rune) rune {
if unicode.IsPrint(r) && r != ' ' {
//Do not count space as well
return r
}
return -1
}, str)
if len(str) > 0 {
return true
}
}
return false
}

0 comments on commit 9b053a3

Please sign in to comment.