This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-app-script.js
65 lines (53 loc) · 2.13 KB
/
google-app-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function insertShortcode(url) {
var shortCode = `[embed src=${url}]`;
var body = DocumentApp.getActiveDocument().getBody();
body.appendParagraph(shortCode); // this appends the shortcode to the end of the document, not really ideal
}
function onOpen() {
DocumentApp.getUi() // Or SpreadsheetApp or SlidesApp or FormApp.
.createMenu('Embed')
.addItem('Embed Content from URL', 'showPrompt')
.addToUi();
}
function replaceSelectedText() {
var selection = DocumentApp.getActiveDocument().getSelection()
if (selection) {
var elements = selection.getRangeElements();
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
// Only modify elements that can be edited as text; skip images and other non-text elements.
if (element.getElement().editAsText) {
var text = element.getElement().editAsText();
var url = text.getText();
var shortCode = `[embed src=${url}]`;
// Bold the selected part of the element, or the full element if it's completely selected.
// does it being partial matter?
if (element.isPartial()) {
DocumentApp.getUi().alert(`selection is partial, not entire element ${element.getStartOffset()} ${element.getEndOffsetInclusive()}`);
}
text.setText(shortCode);
}
}
}
}
function showPrompt() {
var ui = DocumentApp.getUi(); // Same variations.
var result = ui.prompt(
'Embed from social',
'Enter the URL of the content you\'d like to embed below:',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var url = result.getResponseText();
if (button == ui.Button.OK) {
// User clicked "OK".
insertShortcode(url);
ui.alert('Inserted a shortcode to embed the content at ' + url + '.');
} else if (button == ui.Button.CANCEL) {
// User clicked "Cancel".
ui.alert('I didn\'t get a URL.');
} else if (button == ui.Button.CLOSE) {
// User clicked X in the title bar.
ui.alert('You closed the dialog.');
}
}