Created
March 20, 2024 18:38
-
-
Save bennettscience/193064292bb043970c2270ff641b3799 to your computer and use it in GitHub Desktop.
Automatically get a bit.ly link for a Google Form from a linked Google Sheet
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
// Copyright 2017 Brian E. Bennett | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software | |
// and associated documentation files (the "Software"), to deal in the Software without restriction, | |
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, | |
// subject to the following conditions: | |
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | |
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
function setup() { | |
var ui = SpreadsheetApp.getUi(); | |
var prompt = ui.prompt("Bitly API Key", "You need an API key. \nFollow the instructions here: https://support.bitly.com/hc/en-us/articles/230647907-How-do-I-find-my-OAuth-access-token- \n\nPaste your key in below.", ui.ButtonSet.OK_CANCEL); | |
if(prompt.getSelectedButton() == ui.Button.OK) { | |
var key = prompt.getResponseText(); | |
PropertiesService.getDocumentProperties().setProperty("APIkey", key); | |
} else { return } | |
} | |
/************************ Add Menu *******************************/ | |
function onOpen(e) { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu("Bitly Link") | |
.addItem("Setup", "setup") | |
.addItem("Show link", "showPopup") | |
.addToUi(); | |
} | |
/************************ Show Popup *******************************/ | |
// Display a popup window with a shortlink and QR code | |
/*******************************************************************/ | |
function showPopup() { | |
var accessToken = PropertiesService.getDocumentProperties().getProperty("APIkey"); | |
if(accessToken.length === 0) { | |
return Browser.msgBox("Error", "You haven't set an API key. Please run setup and then try again.", SpreadsheetApp.getUi().ButtonSet.OK); | |
} else { | |
var html = HtmlService.createTemplateFromFile('popup').evaluate().setHeight(500).setWidth(600); | |
return SpreadsheetApp.getUi().showModalDialog(html, 'Feedback Link'); | |
} | |
} | |
/************************ Get Shortlink *******************************/ | |
// Called from the popup, get the shortcode from the bitly API | |
// To get your token: https://support.bitly.com/hc/en-us/articles/230647907-How-do-I-find-my-OAuth-access-token- | |
// Add your API key on line 40 | |
/**********************************************************************/ | |
function getShortLink() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var accessToken = PropertiesService.getDocumentProperties().getProperty("APIkey"); | |
var encoded = encodeURIComponent(ss.getFormUrl()); | |
var params = { method: 'GET' } | |
var url = "https://api-ssl.bitly.com/v3/shorten?access_token=" + accessToken + "&longUrl=" + encoded; | |
try { | |
var resp = UrlFetchApp.fetch(url, params); | |
if(resp) { | |
var resp = JSON.parse(resp); | |
return resp.data.url; | |
} | |
} catch(e) { | |
SpreadsheetApp.getUi().alert("bit.ly error", e.message, SpreadsheetApp.getUi().ButtonSet.OK); | |
} | |
} |
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
// display the collected bit.ly shortcode in a popup in the sheet. | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<base target="_top"> | |
<style> | |
#link { width: 100%; font-family: monospace; font-size: 36px; font-align: center; } | |
#qrContainer { display:block; width: 100%; height: auto; } | |
#qr { display: block; width: 60%; height:auto; margin: 10px auto 0; } | |
</style> | |
</head> | |
<body> | |
<div id="link"><?!= getShortLink(); ?></div> | |
<div id="qrContainer"><img id="qr" src="" /></div> | |
<script> | |
document.addEventListener("DOMContentLoaded", function (e) { | |
google.script.run.withSuccessHandler(insertImg).getShortLink(); | |
}); | |
function insertImg(url) { | |
var url = encodeURIComponent(url); | |
var src = "https://api.qrserver.com/v1/create-qr-code/?data=" + url + "&size=250x250" | |
document.getElementById("qr").src = src; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment