Last active
October 3, 2021 19:40
-
-
Save clairefro/74a98d1eb26a3f82452ffd10d7b33443 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Run this ONCE to install make the app listen for the form submission trigger | |
| function installTrigger() { | |
| ScriptApp.newTrigger('onFormSubmit') | |
| .forSpreadsheet(SpreadsheetApp.getActive()) | |
| .onFormSubmit() | |
| .create(); | |
| } | |
| // Used for manual testing only. Simulates a real form submission | |
| 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) | |
| } | |
| // Define the indexes of your header columns (NOTE: these are NOT zero-indexed) | |
| 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] | |
| // get row of submission in sheet | |
| const { rowStart } = e.range | |
| try { | |
| // fetch user data from github | |
| const url = `https://api.github.com/users/${username}`; | |
| const response = UrlFetchApp.fetch(url/*, options*/); | |
| 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) { | |
| // handle any errors | |
| 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