Skip to content

Instantly share code, notes, and snippets.

@clairefro
Created March 22, 2024 16:45
Show Gist options
  • Select an option

  • Save clairefro/b19a2a673972764d7124973f42be50f9 to your computer and use it in GitHub Desktop.

Select an option

Save clairefro/b19a2a673972764d7124973f42be50f9 to your computer and use it in GitHub Desktop.
Google Apps Script x Postman demo
function installTrigger() {
ScriptApp.newTrigger('onFormSubmit')
.forSpreadsheet(SpreadsheetApp.getActive())
.onFormSubmit()
.create();
}
const fakeSubmission = {"authMode":"FULL","namedValues":{"Your Name":["Claire"],"GitHub username":["clairefro"],"Timestamp":["9/20/2021 9:55:20"]},"range":{"columnEnd":3,"columnStart":1,"rowEnd":3,"rowStart":3},"source":{},"triggerUid":"3209236967361824985","values":["9/20/2021 9:55:20","Claire","clairefro"]}
function testSubmission() {
onFormSubmit(fakeSubmission)
}
const AVATAR_URL_COL = 4
const LOCATION_COL = 5
const FOLLOWERS_COL = 6
const PUBLIC_REPOS_COL = 7
function onFormSubmit(e) {
// get username
const responses = e.namedValues
const username = responses["GitHub username"][0]
const { rowStart } = e.range
// fetch user data from github
const options = {
method: "GET",
contentType: "application/json",
muteHttpExceptions: true
};
try {
const url = `https://api.github.com/users/${username}`;
const response = UrlFetchApp.fetch(url/*, options*/);
// if(response.getResponseCode() > 299) throw new Error(`GitHub responded with code: ${response.getResponseCode()}`)
const parsedResponse = JSON.parse(response)
// write data to sheet
const { avatar_url, location, followers, public_repos } = parsedResponse
writeCell(rowStart, AVATAR_URL_COL, avatar_url)
writeCell(rowStart, LOCATION_COL, location)
writeCell(rowStart, FOLLOWERS_COL, followers)
writeCell(rowStart, PUBLIC_REPOS_COL, public_repos)
} catch(err) {
Logger.log(err.message || err)
writeCell(rowStart, AVATAR_URL_COL, "ERROR")
writeCell(rowStart, LOCATION_COL, "ERROR")
writeCell(rowStart, FOLLOWERS_COL, "ERROR")
writeCell(rowStart, PUBLIC_REPOS_COL, "ERROR")
}
}
function writeCell(row, col, val) {
const ss = SpreadsheetApp.getActive();
const s = ss.getSheets()[0]
s.getRange(row, col).setValue(val)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment