After create spreadsheet is not posible to retrieve it (so is not creating the sheet)
Closed this issue · 5 comments
Hi,
I'm making a little service to write on google spreadsheet and works great (kudos great library) but I want to check if the spread name exist, if so no problem, but when not I want to create a new spreadsheet with the send name.
To do so I create a recover in the main method to avoid panic (and it work) but when the method which create the new spread is called works until I try to retrieve the created one by name.
I show you the whole code and the error message in the console, thanks.
The Code:
package process
import (
"fmt"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"gopkg.in/Iwark/spreadsheet.v2"
"io/ioutil"
)
func AddRecords(collection OfferCollection) {
sheet := initSpreadsheet(collection.StoreName)
var nextRowPos = getNextEmptyRow(sheet)
for i, offer := range collection.OfferDataCollection {
if searchLastCode(sheet, offer) {
SendEmail(offer)
}
sheet.Update(nextRowPos + i, 0, collection.Date)
sheet.Update(nextRowPos + i, 1, offer.Name)
sheet.Update(nextRowPos + i, 2, offer.Code)
sheet.Update(nextRowPos + i, 3, offer.StorePrice)
sheet.Update(nextRowPos + i, 4, offer.InetPrice)
sheet.Update(nextRowPos + i, 5, offer.InetOfferPrice)
sheet.Update(nextRowPos + i, 6, offer.Uri)
}
// Make sure call Synchronize to reflect the changes
err := sheet.Synchronize()
checkError(err)
}
func initSpreadsheet(sheetTitle string) *spreadsheet.Sheet {
var sheet *spreadsheet.Sheet
data, err := ioutil.ReadFile("golang-offersCrawler-f4d392a1af9b.json")
checkError(err)
conf, err := google.JWTConfigFromJSON(data, spreadsheet.Scope)
checkError(err)
client := conf.Client(context.TODO())
service := spreadsheet.NewServiceWithClient(client)
defer func() {
if err := recover(); err != nil {
sheet = createNewSheet(sheetTitle, service)
}
}()
sheet = findSheetByTitle(sheetTitle, service)
return sheet
}
func fetchSpreadsheet(service *spreadsheet.Service) spreadsheet.Spreadsheet {
spreadsheet, err := service.FetchSpreadsheet("11y4NmD39gPSPcKAzMU0lsAoDWVQsXOVxJNNVZXV7xek")
checkError(err)
return spreadsheet
}
func findSheetByTitle(sheetTitle string, service *spreadsheet.Service) *spreadsheet.Sheet {
fmt.Println("findSheetByTitle: ", sheetTitle)
spreadsheet := fetchSpreadsheet(service)
sheet, err := spreadsheet.SheetByTitle(sheetTitle)
checkError(err)
return sheet
}
func createNewSheet(sheetTitle string, service *spreadsheet.Service) *spreadsheet.Sheet {
fmt.Println("createNewSheet: ", sheetTitle)
ss, err := service.CreateSpreadsheet(spreadsheet.Spreadsheet{
Properties: spreadsheet.Properties{
Title: sheetTitle,
},
})
checkError(err)
sheet, err := ss.SheetByTitle(sheetTitle)
checkError(err)
return sheet
}
func getNextEmptyRow(sheet *spreadsheet.Sheet) int {
var counter uint
for _, row := range sheet.Rows {
for _, cell := range row {
counter = cell.Row
}
}
return int(counter + 1)
}
func searchLastCode(sheet *spreadsheet.Sheet, data OfferData) bool {
var match bool
for i := range sheet.Rows {
if sheet.Rows[i][2].Value == data.Code {
match = false
if sheet.Rows[i][5].Value != data.InetOfferPrice {
match = true
}
}
}
return match
}
func checkError(err error) {
if err != nil {
fmt.Println("error: ", err)
panic(err.Error())
}
}
Console:
findSheetByTitle: Audiomusicax
error: sheet not found by the title
createNewSheet: Audiomusicax
error: sheet not found by the title
panic: sheet not found by the title [recovered]
panic: sheet not found by the title
goroutine 1 [running]:
searchOffers/process.checkError(0x7625e0, 0xc4202a2510)
/home/alejo/go/src/searchOffers/process/spreadsheet.go:127 +0xec
searchOffers/process.createNewSheet(0xc42001a794, 0xc, 0xc4202c64a0, 0x4b725e)
/home/alejo/go/src/searchOffers/process/spreadsheet.go:88 +0x219
searchOffers/process.initSpreadsheet.func1(0xc42001a794, 0xc, 0xc4202c64a0, 0xc420153c00)
/home/alejo/go/src/searchOffers/process/spreadsheet.go:50 +0x61
panic(0x6b2e40, 0xc42028c590)
/usr/local/go/src/runtime/panic.go:502 +0x229
searchOffers/process.checkError(0x7625e0, 0xc42028c580)
/home/alejo/go/src/searchOffers/process/spreadsheet.go:127 +0xec
searchOffers/process.findSheetByTitle(0xc42001a794, 0xc, 0xc4202c64a0, 0xc)
/home/alejo/go/src/searchOffers/process/spreadsheet.go:72 +0x154
searchOffers/process.initSpreadsheet(0xc42001a794, 0xc, 0x0)
/home/alejo/go/src/searchOffers/process/spreadsheet.go:54 +0x20a
searchOffers/process.AddRecords(0xc42001a794, 0xc, 0xc4200168c0, 0x15, 0xc4201aa700, 0x5, 0x8)
/home/alejo/go/src/searchOffers/process/spreadsheet.go:13 +0x50
main.main()
/home/alejo/go/src/searchOffers/main.go:25 +0x1f8
Process finished with exit code 2
Sorry I've been under the ground for a while...
Thank you for your reporting!
I'll take a look at it.
@kanodoe Are you trying te create a new sheet or a new spreadsheet?
It looks like the function createNewSheet
creates a new spreadsheet with a provided name and then you try to fetch a sheet of the created spreadsheet by the name of the spreadsheet
?
Hi @mcnijman I was trying to create a new sheet (in the already created and configured spreadsheet) if the name wasn't available when I check it.
Your code creates a new spreadsheet (not a new sheet). I don't think this is implemented.
Oh... I see, then I understand wrong (ups!).
Maybe if I've time I could try to make a fork and implement this feature, thanks for your answer.