Google Apps Script Document Projects
Add Date into Google Doc at cursor
Adding images from the web and Drive to Google Docs
Copy All Images from Doc into your Google Drive
Highlight emails in a Doc using Regex on Doc Body Contents
Add Date into Google Doc at cursor
Add Date into Google Doc at cursor Document Apps Script Example
function onOpen() {
Laurence Svekis Courses https://basescripts.com/
1
const ui = DocumentApp.getUi();
ui.createMenu('Adv')
.addItem('Add Date','adder')
.addToUi()
function adder(){
const doc = DocumentApp.getActiveDocument();
const cur = doc.getCursor();
if(cur){
const val = new Date();
const temp = Utilities.formatDate(val,"GMT",
"yyyy-MM-dd")
const ele = cur.insertText(temp);
Laurence Svekis Courses https://basescripts.com/
2
Copy All Images from Doc into your Google Drive
Example will extract all the images from a Google Doc and make copies of them directly into
your Google Drive.
function onOpen(){
Laurence Svekis Courses https://basescripts.com/
3
DocumentApp.getUi()
.createMenu('images')
.addItem('creator','getImgs')
.addToUi()
function getImgs(){
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const images = body.getImages();
const id = '1mW6Yh2X4wU8A-vQs1Z2RRVHIfHD_SI3u';
const folder = DriveApp.getFolderById(id);
images.forEach((img,ind) =>{
const blob = img.getBlob().copyBlob();
blob.setName(`Image
${ind+1}.jpg`).setContentTypeFromExtension();
folder.createFile(blob);
})
//Logger.log(images);
Laurence Svekis Courses https://basescripts.com/
4
Adding images from the web and Drive to Google
Docs
UI menu item to dynamically add images like logos and others. Press the menu button and add
an image. Images can be from Google Drive or from the web. Preselected images within you
Doc.
function onOpen() {
DocumentApp.getUi().createMenu('adder')
.addItem('Logo','addLogo1')
.addItem('Logo GDrive','addLogo2')
.addItem('Svekis','addLogo3')
.addToUi();
Laurence Svekis Courses https://basescripts.com/
5
}
function addLogo3(){
const cur =
DocumentApp.getActiveDocument().getCursor();
if(cur){
cur.insertInlineImage(DriveApp.getFileById('14aV3w
xHA--YTFr6zA3R0pz_WYPasE4Bt').getBlob());
function addLogo2(){
const cur =
DocumentApp.getActiveDocument().getCursor();
if(cur){
const id = '1DEmp6fkvHox2vh1LgquSJylmwhfIMETG';
const blob =
DriveApp.getFileById(id).getBlob();
Laurence Svekis Courses https://basescripts.com/
6
cur.insertInlineImage(blob);
function addLogo1(){
const cur =
DocumentApp.getActiveDocument().getCursor();
if(cur){
const url =
'https://www.discoveryvip.com/img/d.png';
const blob = UrlFetchApp.fetch(url).getBlob();
cur.insertInlineImage(blob);
Logger.log(blob);
Laurence Svekis Courses https://basescripts.com/
7
Highlight emails in a Doc using Regex on Doc Body
Contents
Select all matching results from a Regex for email patterns. Creates an array of the emails
contained in the doc, then applies styling to the email. Highlight the matching results with a
yellow background and black text.
function onOpen(){
DocumentApp.getUi()
.createMenu('emails')
.addItem('Highlight','highlighter')
.addToUi()
Laurence Svekis Courses https://basescripts.com/
8
function highlighter(){
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const bodyText = body.getText();
const exp =
/([A-Za-z0-9._+-]+@[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+)/
gi;
const results = bodyText.match(exp);
const style = {};
style[DocumentApp.Attribute.FOREGROUND_COLOR] =
'#000000';
style[DocumentApp.Attribute.BACKGROUND_COLOR] =
'#FFFF00';
const paras = body.getParagraphs();
paras.forEach(p =>{
results.forEach(email =>{
const textLoc = p.findText(email);
if(textLoc != null && textLoc.getStartOffset()
!= -1){
Laurence Svekis Courses https://basescripts.com/
9
textLoc.getElement().setAttributes(textLoc.getStartOf
fset(),textLoc.getEndOffsetInclusive(),style);
})
})
//Logger.log(paras);
Laurence Svekis Courses https://basescripts.com/
10