Initial Commit
This commit is contained in:
commit
e240129bfb
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Configuration File #
|
||||
######################
|
||||
Config.gs
|
99
QR.gs
Normal file
99
QR.gs
Normal file
@ -0,0 +1,99 @@
|
||||
var width = 150;
|
||||
var widthStr = (width * 10).toString();
|
||||
|
||||
//When the script is installed, create our extension menu
|
||||
function onInstall() {
|
||||
createUI();
|
||||
}
|
||||
|
||||
//When the script is opened, create our extension menu
|
||||
function onOpen() {
|
||||
createUI();
|
||||
}
|
||||
|
||||
function createUI(){
|
||||
var ui = DocumentApp.getUi();
|
||||
|
||||
//Text to display in UI
|
||||
var ui_text = 'Replace <<QR>> With QR Codes';
|
||||
|
||||
//Exact name of the function to be run when the UI is clicked
|
||||
var ui_function = 'qr';
|
||||
|
||||
//Create the Addon Menu with specified text and function
|
||||
ui.createAddonMenu()
|
||||
.addItem(ui_text, ui_function)
|
||||
.addToUi();
|
||||
}
|
||||
|
||||
function callAPI() {
|
||||
|
||||
//This is the qurl (QR URL) this will be the URL we want the QR code to point to
|
||||
var qurl = DocumentApp.getActiveDocument().getUrl();
|
||||
|
||||
//This is the URL we need to make an HTTP POST request to.
|
||||
var url = "https://api.qr-code-generator.com/v1/create?access-token=" + token;
|
||||
|
||||
//These are the options we send with our POST request.
|
||||
var options = {
|
||||
"method": "post", //We don't need to GET just POST
|
||||
"payload":{ //What are we telling the API
|
||||
"frame_name": "no-frame", //We don't want a frame for our QR Codes
|
||||
"qr_code_text": qurl, //We cant the code to link to the qurl
|
||||
"image_format": "PNG", //Our format is PNG (JPG also works)
|
||||
"qr_code_logo": "no-logo", //No logo (We can't upload an MCLS logo or I would try to do that)
|
||||
"image_width":widthStr, //Image Width and Height (QR codes are squares so this is fine)
|
||||
"download":1 //Data return type, leave this as 1
|
||||
}
|
||||
};
|
||||
|
||||
//Make our HTTP POST request using UrlFetchApp and return the Binary Image Data
|
||||
var response = UrlFetchApp.fetch(url, options);
|
||||
return response;
|
||||
}
|
||||
|
||||
function qr(){
|
||||
|
||||
/*
|
||||
https://stackoverflow.com/questions/74877202/convert-binary-image-to-image-and-insert-into-google-doc-google-apps-script
|
||||
*/
|
||||
|
||||
//Replace specified searchText with a specified Binary Image Data (image)
|
||||
var replaceTextToImage = function (body, searchText, image, width) {
|
||||
var next = body.findText(searchText);
|
||||
if (!next) return;
|
||||
var r = next.getElement(); //Get the next instance of searchText from our body
|
||||
r.asText().setText(""); //Replace the text with a blank strink
|
||||
var img = r.getParent().asParagraph().insertInlineImage(0, image); //Convert Binary Image Data to InlineImage data
|
||||
if (width && typeof width == "number") { //Set image dimensions
|
||||
var w = img.getWidth();
|
||||
var h = img.getHeight();
|
||||
img.setWidth(width);
|
||||
img.setHeight(width * h / w);
|
||||
}
|
||||
return next; //Return the element with the InlineImage data
|
||||
};
|
||||
|
||||
//This is the text we will be replacing with our QR code
|
||||
var replaceText = "<<QR>>";
|
||||
|
||||
var doc = DocumentApp.getActiveDocument(); //Get the current document
|
||||
var body = doc.getBody(); //Get the Body
|
||||
var head = doc.getHeader(); //Header
|
||||
var foot = doc.getFooter(); //and Footer sections of the document
|
||||
|
||||
var code = callAPI(); //Call our API and get our QR code image as Binary Image Data
|
||||
var blob = code.getBlob(); //Create a blob from that data
|
||||
|
||||
do { //Replaces our replaceText with our image in the Header of the document
|
||||
var next = replaceTextToImage(head, replaceText, blob, width);
|
||||
} while (next);
|
||||
|
||||
do { //Replaces our replaceText with our image in the Body of the document
|
||||
var next = replaceTextToImage(body, replaceText, blob, width);
|
||||
} while (next);
|
||||
|
||||
do { //Replaces our replaceText with our image in the Footer of the document
|
||||
var next = replaceTextToImage(foot, replaceText, blob, width);
|
||||
} while (next);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user