-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from StanGirard/feat/gsheet
Feat/gsheet
- Loading branch information
Showing
13 changed files
with
732 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.6.0 | ||
0.7.0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package gsheet | ||
|
||
import ( | ||
"google.golang.org/api/sheets/v4" | ||
) | ||
|
||
func createBasicChartSeries(values [][]interface{}) []*sheets.BasicChartSeries { | ||
var BasicChartSeries []*sheets.BasicChartSeries | ||
for i := range values { | ||
if i == 0 { | ||
continue | ||
} | ||
BasicChartSeries = append(BasicChartSeries, &sheets.BasicChartSeries{ | ||
Series: &sheets.ChartData{ | ||
SourceRange: &sheets.ChartSourceRange{ | ||
Sources: []*sheets.GridRange{ | ||
{ //A2:O2 | ||
SheetId: 1023, | ||
StartRowIndex: int64(i), | ||
EndRowIndex: int64(i + 1), | ||
StartColumnIndex: 0, | ||
EndColumnIndex: int64(len(values[i])), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
return BasicChartSeries | ||
} | ||
|
||
func createHistogramSeries(values [][]interface{}) []*sheets.HistogramSeries { | ||
var HistogramSeries []*sheets.HistogramSeries | ||
for i := range values { | ||
if i == 0 { | ||
continue | ||
} | ||
HistogramSeries = append(HistogramSeries, &sheets.HistogramSeries{ | ||
Data: &sheets.ChartData{ | ||
SourceRange: &sheets.ChartSourceRange{ | ||
Sources: []*sheets.GridRange{ | ||
{ //A2:O2 | ||
SheetId: 1023, | ||
StartRowIndex: int64(i), | ||
EndRowIndex: int64(i + 1), | ||
StartColumnIndex: 0, | ||
EndColumnIndex: int64(len(values[i])), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
return HistogramSeries | ||
} | ||
|
||
func createBasicChartSpec(legendPosition, chartype, stackedType string, sourceSheetId int64, values [][]interface{}, basicChartSeries []*sheets.BasicChartSeries) *sheets.BasicChartSpec { | ||
var BasicChartSpec *sheets.BasicChartSpec | ||
BasicChartSpec = &sheets.BasicChartSpec{ | ||
HeaderCount: 1, | ||
Series: basicChartSeries, | ||
LegendPosition: "BOTTOM_LEGEND", | ||
ChartType: "COLUMN", | ||
StackedType: "STACKED", | ||
Domains: []*sheets.BasicChartDomain{ | ||
{ | ||
Domain: &sheets.ChartData{ | ||
SourceRange: &sheets.ChartSourceRange{ | ||
Sources: []*sheets.GridRange{ | ||
{ | ||
SheetId: 1023, | ||
StartRowIndex: 0, | ||
EndRowIndex: 1, | ||
StartColumnIndex: 0, | ||
EndColumnIndex: int64(len(values[0])), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
return BasicChartSpec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package gsheet | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/stangirard/pricy/internal/helpers" | ||
) | ||
|
||
var ( | ||
spreadsheet = flag.String("spreadsheet", "", "The ID of the spreadsheet to write to.") | ||
) | ||
|
||
func Execute(value [][]string) { | ||
flag.Parse() | ||
|
||
dataSpreadsheet := helpers.ConvertStringToInterface(value) | ||
srv, ctx := NewSheetService() | ||
|
||
// Create a new spreadsheet. | ||
rb := createSpreadSheetConfig() | ||
|
||
BasicChartSeries := createBasicChartSeries(dataSpreadsheet) | ||
|
||
if *spreadsheet == "" { | ||
*spreadsheet = createSpreadsheet(srv, rb, ctx) | ||
// Add a sheet | ||
addSheet(srv, *spreadsheet, "Reports", 1023) | ||
deleteSheet(srv, *spreadsheet, 0) | ||
|
||
} | ||
|
||
//Delete the Chart sheet if it exists | ||
deleteSheet(srv, *spreadsheet, 1024) | ||
addSheet(srv, *spreadsheet, "Charts", 1024) | ||
|
||
// Write the value variable to the new spreadsheet. | ||
writeCSVToSheet(ctx, srv, *spreadsheet, dataSpreadsheet) | ||
|
||
// Create BasicChart Series | ||
chartSpecs := createBasicChartSpec("BOTTOM_LEGEND", "COLUMN", "STACKED", 1023, dataSpreadsheet, BasicChartSeries) | ||
addChart(srv, *spreadsheet, 1024, chartSpecs) | ||
|
||
// Print the spreadsheet url | ||
fmt.Printf("Spreadsheet URL: https://docs.google.com/spreadsheets/d/%s\n", *spreadsheet) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package gsheet | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"google.golang.org/api/sheets/v4" | ||
) | ||
|
||
func createSpreadSheetConfig() *sheets.Spreadsheet { | ||
var timeNowString = time.Now().Format("2006-01-02-15-04-05") | ||
title := "Pricy-Report-" + timeNowString | ||
rb := &sheets.Spreadsheet{ | ||
Properties: &sheets.SpreadsheetProperties{ | ||
Title: title, | ||
}, | ||
} | ||
return rb | ||
} | ||
|
||
func addSheet(service *sheets.Service, spreadsheetId, name string, idSheet int64) { | ||
_, error := service.Spreadsheets.BatchUpdate(*spreadsheet, &sheets.BatchUpdateSpreadsheetRequest{ | ||
Requests: []*sheets.Request{ | ||
{ | ||
AddSheet: &sheets.AddSheetRequest{ | ||
Properties: &sheets.SheetProperties{ | ||
Title: name, | ||
SheetId: idSheet, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}).Do() | ||
|
||
if error != nil { | ||
log.Fatalf("Unable to add sheet: %v", error) | ||
} | ||
fmt.Println("Sheet added: ", name) | ||
} | ||
|
||
func addChart(service *sheets.Service, spreadsheetId string, idSheetToAddTo int64, specs *sheets.BasicChartSpec) { | ||
_, error := service.Spreadsheets.BatchUpdate(*spreadsheet, &sheets.BatchUpdateSpreadsheetRequest{ | ||
Requests: []*sheets.Request{ | ||
{ | ||
AddChart: &sheets.AddChartRequest{ | ||
Chart: &sheets.EmbeddedChart{ | ||
Position: &sheets.EmbeddedObjectPosition{ | ||
NewSheet: false, | ||
OverlayPosition: &sheets.OverlayPosition{ | ||
AnchorCell: &sheets.GridCoordinate{ | ||
SheetId: 1024, | ||
RowIndex: 0, | ||
ColumnIndex: 0, | ||
}, | ||
WidthPixels: 1200, | ||
HeightPixels: 600, | ||
}, | ||
}, | ||
Spec: &sheets.ChartSpec{ | ||
BasicChart: specs, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}).Do() | ||
|
||
if error != nil { | ||
log.Fatalf("Unable to add sheet: %v", error) | ||
} | ||
fmt.Println("Chart added") | ||
} | ||
|
||
func deleteSheet(service *sheets.Service, spreadsheetId string, sheetId int64) { | ||
_, error := service.Spreadsheets.BatchUpdate(*spreadsheet, &sheets.BatchUpdateSpreadsheetRequest{ | ||
Requests: []*sheets.Request{ | ||
{ | ||
DeleteSheet: &sheets.DeleteSheetRequest{ | ||
SheetId: sheetId, | ||
}, | ||
}, | ||
}, | ||
}).Do() | ||
|
||
if error != nil { | ||
fmt.Printf("Sheet not deleted because it does not exist (Id:%v)\n", sheetId) | ||
} | ||
fmt.Println("Default sheet deleted") | ||
} | ||
|
||
func writeCSVToSheet(context context.Context, service *sheets.Service, spreadsheetId string, data [][]interface{}) { | ||
valuesRanges := make([]*sheets.ValueRange, len(data)) | ||
valuesRanges = append(valuesRanges, &sheets.ValueRange{ | ||
MajorDimension: "ROWS", | ||
Values: data, | ||
Range: "A1:ZZ", | ||
}, | ||
) | ||
rb2 := &sheets.BatchUpdateValuesRequest{ | ||
ValueInputOption: "USER_ENTERED", | ||
Data: valuesRanges, | ||
} | ||
|
||
_, err := service.Spreadsheets.Values.BatchUpdate(*spreadsheet, rb2).Context(context).Do() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println("Sheet populated") | ||
} | ||
|
||
func createSpreadsheet(service *sheets.Service, config *sheets.Spreadsheet, ctx context.Context) string { | ||
resp, err := service.Spreadsheets.Create(config).Context(ctx).Do() | ||
if err != nil { | ||
log.Fatalf("Unable to create spreadsheet: %v", err) | ||
} | ||
return resp.SpreadsheetId | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package gsheet | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
|
||
"golang.org/x/oauth2/google" | ||
"google.golang.org/api/option" | ||
"google.golang.org/api/sheets/v4" | ||
) | ||
|
||
func NewSheetService() (*sheets.Service, context.Context) { | ||
|
||
ctx := context.Background() | ||
//Os get env variable GOOGLE_APPLICATION_CREDENTIALS | ||
GOOGLE_APPLICATION_CREDENTIALS := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") | ||
|
||
b, err := ioutil.ReadFile(GOOGLE_APPLICATION_CREDENTIALS) | ||
if err != nil { | ||
log.Fatalf("Unable to read client secret file: %v", err) | ||
} | ||
// If modifying these scopes, delete your previously saved token.json. | ||
config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets") | ||
if err != nil { | ||
log.Fatalf("Unable to parse client secret file to config: %v", err) | ||
} | ||
client := getClient(config) | ||
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client)) | ||
if err != nil { | ||
log.Fatalf("Unable to retrieve Sheets client: %v", err) | ||
} | ||
|
||
return srv, ctx | ||
} |
Oops, something went wrong.