-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReadSpreadsheet improvements #64
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const { GoogleSpreadsheet } = require('google-spreadsheet') | ||
const { JWT } = require('google-auth-library') | ||
|
||
const extension = require('./../extension') | ||
|
||
|
@@ -14,33 +15,35 @@ class Header { | |
this.name = suffix[1] | ||
} | ||
|
||
parse(val) { | ||
parse(value) { | ||
let val = value || '' | ||
if (this.type == 'number') { | ||
return +val | ||
} | ||
if (this.type == 'list') { | ||
return val.split(', ') | ||
return val.split(',').map(s => s.trim()) | ||
} | ||
if (val === null) { | ||
return '' | ||
} | ||
return val | ||
return val.trim() | ||
} | ||
|
||
get(row) { | ||
return this.parse(row[this.value]) | ||
return this.parse(row.get(this.value)) | ||
} | ||
|
||
getIdentifier(person) { | ||
return this.parse(person[this.name]) | ||
} | ||
} | ||
|
||
readSpreadsheetImpl = async function(competition, spreadsheetId) { | ||
readSpreadsheetImpl = async function(competition, spreadsheetId, offset) { | ||
out = { warnings: [], loaded: 0 } | ||
const creds = require('./../google-credentials.json') | ||
const doc = new GoogleSpreadsheet(spreadsheetId) | ||
await doc.useServiceAccountAuth(creds) | ||
const serviceAccountAuth = new JWT({ | ||
email: creds.client_email, | ||
key: creds.private_key, | ||
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'], | ||
}) | ||
const doc = new GoogleSpreadsheet(spreadsheetId, serviceAccountAuth) | ||
await doc.loadInfo() | ||
const sheet = doc.sheetsByIndex[0] | ||
await sheet.loadHeaderRow(1) | ||
|
@@ -59,7 +62,7 @@ readSpreadsheetImpl = async function(competition, spreadsheetId) { | |
}) | ||
}) | ||
|
||
const rows = await sheet.getRows({offset: 1}) | ||
const rows = await sheet.getRows({ offset }) | ||
rows.forEach((row) => { | ||
// First use the identifiers provided to find the person. | ||
var bestMatch = 0 | ||
|
@@ -73,10 +76,17 @@ readSpreadsheetImpl = async function(competition, spreadsheetId) { | |
return | ||
} | ||
var identifierVal = header.get(row) | ||
if (!identifierVal) { | ||
return | ||
} | ||
identifierVal = identifierVal instanceof String ? identifierVal.toUpperCase() : identifierVal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to use this "trick" to deal with non-string identifiers (ie: wcaUserId). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good call. I wasn't using non-string identifiers when I did this (only email, wcaId, name). |
||
|
||
if (firstIdentifier === '') { | ||
firstIdentifier = identifierVal | ||
} | ||
if (header.getIdentifier(person).toUpperCase() === identifierVal.toUpperCase()) { | ||
var personIdentifierVal = header.getIdentifier(person) | ||
personIdentifierVal = personIdentifierVal instanceof String ? personIdentifierVal.toUpperCase() : personIdentifierVal | ||
if (personIdentifierVal === identifierVal) { | ||
matching++ | ||
} | ||
}) | ||
|
@@ -117,12 +127,18 @@ const ReadSpreadsheet = { | |
name: 'spreadsheetId', | ||
type: 'String', | ||
}, | ||
{ | ||
name: 'offset', | ||
type: 'Number', | ||
docs: 'Skip the first `offset` rows of the spreadsheet.', | ||
defaultValue: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does change the existing behavior! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is a better default, thanks! I'll set this in my scripts this year. |
||
}, | ||
], | ||
outputType: 'ReadSpreadsheetResult', | ||
usesContext: true, | ||
mutations: ['persons'], | ||
implementation: (ctx, spreadsheetId) => { | ||
return readSpreadsheetImpl(ctx.competition, spreadsheetId) | ||
implementation: (ctx, spreadsheetId, offset) => { | ||
return readSpreadsheetImpl(ctx.competition, spreadsheetId, offset) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This I could not explain, but
row[headerValue]
would return undefined; and the documentation here mentions.get
, which does work in this case.Not sure why it worked before, but do you see any reason to stick to
[]
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea. If this works then great :)