Skip to content
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

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions functions/sheets.js
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')

Expand All @@ -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))
Copy link
Contributor Author

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 []?

Copy link
Collaborator

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 :)

}

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)
Expand All @@ -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
Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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).

Copy link
Collaborator

Choose a reason for hiding this comment

The 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++
}
})
Expand Down Expand Up @@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does change the existing behavior!
If this is not fine, I can definitely set 1 as the default value and adjust my code :)

Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
}
}

Expand Down
Loading