Google+Apps+Script+Quickstart+Code+Examples
Google+Apps+Script+Quickstart+Code+Examples
Examples
Create a Spreadsheet with rows and columns values from a loop 1
Apps Script UrlFetchApp Get URL Data and output to Web App 3
How to check your daily quota of remaining emails to send in MailApp with
Apps Script 9
Sheet Data as JSON Object connect with JavaScript output into a web page
11
1
Laurence Svekis - https://basescripts.com/
function maker1() {
//const id = '1Q7******v6IPjVU1P';
//const folder = DriveApp.getFolderById(id);
const folder = DriveApp.createFolder('New One');
const sheet = SpreadsheetApp.create('new sheet 2',5,3);
let cell = 1;
for(let row=0;row<5;row++){
const holder = [];
for(let col=0;col<3;col++){
holder.push(`Cell ${cell} Col ${col+1} Row ${row+1}`);
cell++;
}
sheet.appendRow(holder);
}
Logger.log(sheet.getUrl());
const sheetid = sheet.getId();
const file = DriveApp.getFileById(sheetid);
file.moveTo(folder);
}
2
Laurence Svekis - https://basescripts.com/
Use array data to create a spreadsheet and
populate the values
function maker2(){
const sheet = SpreadsheetApp.create('data',3,2);
sheet.appendRow(['one','two']);
sheet.appendRow(['three','four']);
sheet.appendRow(['five','six']);
}
3
Laurence Svekis - https://basescripts.com/
function doGet(){
const html = getData();
//return ContentService.createTextOutput(html);
return HtmlService.createHtmlOutput(html);
}
function getData(){
const url = 'http://www.google.com';
const response = UrlFetchApp.fetch(url);
return response.getContentText();
}
4
Laurence Svekis - https://basescripts.com/
function getUsers(){
const results = 10;
const url = 'https://randomuser.me/api/?results='+results;
const res = UrlFetchApp.fetch(url);
//Logger.log(res.getContentText());
const json = JSON.parse(res.getContentText());
//Logger.log(json.results);
const sheet = SpreadsheetApp.create('users');
sheet.appendRow(['First','Last','Country','Email']);
json.results.forEach(item =>{
const u = item.name;
const l = item.location;
sheet.appendRow([u.first,u.last,l.country,item.email]);
const user = `${u.title} ${u.first} ${u.last}`;
Logger.log(user);
})
}
5
Laurence Svekis - https://basescripts.com/
How to send data to an endpoint UrlFetchApp
POST JSON
Use of https://httpbin.org/#/HTTP_Methods/get_get and post to test fetch request.
function sender1(){
const url = 'https://httpbin.org/post';
const vals = {
'first' : 'Laurence',
'last' : 'Svekis',
'id' : 100
}
const strVals = JSON.stringify(vals);
const opts = {
'method' : 'post',
'contentType' : 'application/json',
'payload' : strVals
}
const rep = UrlFetchApp.fetch(url,opts);
const data = rep.getContentText();
const obj = JSON.parse(data);
6
Laurence Svekis - https://basescripts.com/
Logger.log(obj.json);
const sheet = SpreadsheetApp.create('JSON');
sheet.appendRow(['First','Last','ID']);
sheet.appendRow([obj.json.first,obj.json.last,obj.json.id]);
}
function creator1(){
const html = '<h1>Laurence Svekis</h1>';
const blob = Utilities.newBlob(html,'text/plain','newfile.txt');
const email = Session.getActiveUser().getEmail();
MailApp.sendEmail(email,'Check it out','Hello There',{
name : 'My File maker',
attachments : [blob.getAs(MimeType.PDF)]
});
}
7
Laurence Svekis - https://basescripts.com/
Advanced options for send emails with
MailApp
Send emails to multiple recipients with a comma separated string containing the
emails.
function creator2(){
const email = Session.getActiveUser().getEmail();
const html = '<h1>Laurence Svekis</h1>';
const emails =
'gapps******[email protected],gap******[email protected],ga******[email protected]
om';
MailApp.sendEmail({
name : 'Laurence',
to : emails,
cc : email,
bcc : email,
replyTo : 'LaurenceSvekis******@basescripts.com',
subject : 'Subject',
htmlBody: html
});
}
8
Laurence Svekis - https://basescripts.com/
How to check your daily quota of remaining
emails to send in MailApp with Apps Script
function checker1(){
const val = MailApp.getRemainingDailyQuota();
Logger.log(val);
}
function makerObj(){
const obj = [
{
first : 'Laurence',
last : 'Svekis',
id : 500
},
{
first : 'Jane',
last : 'Doe',
9
Laurence Svekis - https://basescripts.com/
id : 5
}
];
return obj;
}
function doGet(){
const obj = makerObj();
const output = JSON.stringify(obj);
return
ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JS
ON);
}
10
Laurence Svekis - https://basescripts.com/
Lucas Anderson New Zealand [email protected]
Mustafa Türkdoğan Turkey [email protected]
Ava Graves Ireland [email protected]
Ernest Masson Switzerland [email protected]
Thibault Muller France [email protected]
Gloria Carmona Spain [email protected]
Odila Caldeira Brazil [email protected]
function makeObj(rows,headings){
return rows.map(function(row){
const tempObj = {};
headings.forEach((heading,index)=>{
heading = heading.toLowerCase();
heading = heading.replace(/\s/g, '');
tempObj[heading] = row[index];
})
return tempObj;
})
}
11
Laurence Svekis - https://basescripts.com/
<div class="output">Click</div>
<script src="app.js"></script>
</body>
</html>
JavaScript
const url = 'https://script.google.com/macros/s/AK******/exec';
const output = document.querySelector('.output');
output.onclick = ()=>{
fetch(url)
.then(res => res.json())
.then(data =>{
maker(data);
})
}
function maker(data){
const main = document.createElement('div');
output.append(main);
data.forEach(user=>{
const div = document.createElement('div');
div.innerHTML = `${user.email} ${user.first} ${user.last}
${user.country}`;
main.append(div);
})
}
Apps Script
function sheetData(){
const id = '1JZA6Qi******ZHJ_eA';
const ss = SpreadsheetApp.openById(id);
12
Laurence Svekis - https://basescripts.com/
const sheet = ss.getSheetByName('data');
const data = sheet.getDataRange().getValues();
const headings = data[0];
const rows = data.slice(1);
return (makeObj(rows,headings));
}
function makeObj(rows,headings){
return rows.map(function(row){
const tempObj = {};
headings.forEach((heading,index)=>{
heading = heading.toLowerCase();
heading = heading.replace(/\s/g, '');
tempObj[heading] = row[index];
})
return tempObj;
})
}
function doGet(){
const obj = sheetData();
const output = JSON.stringify(obj);
return
ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JS
ON);
}
13
Laurence Svekis - https://basescripts.com/