0% found this document useful (0 votes)
32 views5 pages

Section 8 - Sheets Data Source Code

This document contains a Google Apps Script for a Google Sheets add-on that creates a custom menu with options to log messages, send emails, and generate PDFs based on user data. It includes functions to handle user selection, create and send PDFs via email, and log actions in a separate sheet. The script also manages user data and interactions with Google Drive and Gmail services.

Uploaded by

electioncelldnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views5 pages

Section 8 - Sheets Data Source Code

This document contains a Google Apps Script for a Google Sheets add-on that creates a custom menu with options to log messages, send emails, and generate PDFs based on user data. It includes functions to handle user selection, create and send PDFs via email, and log actions in a separate sheet. The script also manages user data and interactions with Google Drive and Gmail services.

Uploaded by

electioncelldnh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

function onOpen() {

const ui = SpreadsheetApp.getUi();
ui.createMenu('adv menu')
.addItem('log','myLog')
.addItem('info','myInfo')
.addItem('email','myEmailer')
.addItem('PDF','myPDF')
.addToUi();
}
function doGet(e){
if(e.parameter.id){
sheetLogger(e.parameter.id);
adder(e.parameter.id);
}else{
sheetLogger(JSON.stringify(e));
}
return ContentService.createTextOutput(JSON.stringify(e));
}
function myPDF(){
const data = checkSel();
if(data.sName == 'Users' && data.results){
createmyPDF(data.results);
sheetLogger('PDF Made '+data.results.first);
}else{
SpreadsheetApp.getUi().alert('Sorry no Data!');
}
}
function createmyPDF(user){
const fid = '1pbtYV********a5bgu';
const docID = '1gT********d40fPtO_SX8';
const docTemp = DriveApp.getFileById(docID);
const mainfolder = DriveApp.getFolderById(fid);
const docNew = docTemp.makeCopy(mainfolder);
const editNew = DocumentApp.openById(docNew.getId());
const body = editNew.getBody();
body.replaceText('{first}',user.first);
body.replaceText('{last}',user.last);
editNew.saveAndClose();
const myBlob = docNew.getAs(MimeType.PDF);
const newPDF =
mainfolder.createFile(myBlob).setName(user.first+user.last);

SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Users').getRange
(user.row,6).setValue(newPDF.getUrl());
if(user.email) sendtoEmail(user,newPDF);
docNew.setTrashed(true);
return newPDF;
}
function sendtoEmail(data,newPDF){
const userEmail = Session.getActiveUser().getEmail();
const message = `Complete : ${data.first} ${data.last} Send to
email ${data.email}`;
try {
MailApp.sendEmail(userEmail,'PDF Ready',message,{
attachments:[newPDF]
})
} catch(e){}
}
function adder(myid){
const id = '1xPf8********P_IbIzBLkFw2rd7vuQ';
const sheet = SpreadsheetApp.openById(id).getSheetByName('Users');
const data = sheet.getDataRange().getValues();
for(let i=0;i<data.length;i++){
if(data[i][0] == myid){
const curRange = sheet.getRange(i+1,7,1,1);
let tempVal = curRange.getValue();
tempVal++;
curRange.setValue(tempVal);
}
}
}
function myEmailer(){
const data = checkSel();
if(data.sName == 'Users' && data.results){
sendUser(data.results);
sheetLogger('email sent '+data.results.email);
}else{
SpreadsheetApp.getUi().alert('Sorry no Data!');
}
}
function sendUser(user){
const temp = HtmlService.createTemplateFromFile('temp');
temp.user = user;
let message = temp.evaluate().getContent();
message += 'id:'+user.id;
message += tracker(user.id);
MailApp.sendEmail({
to:user.email,
subject:'Tester Email',
htmlBody:message
})

SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Users').getRange
(user.row,5).setValue('send');
}
function tracker(id){
const url =
'https://script.google.com/macros/s/AKf********F********nRWHbiqEK0T_hK
KtqwWBg/exec';
return "<img src='"+url+"?id="+id+"' width='1' height='1' />";
}
function myLog(){
sheetLogger('Hello World');
}
function myInfo(){
const data = checkSel();
//Logger.log(data);
if(data.sName == 'Users' && data.results){
const html = HtmlService.createTemplateFromFile('temp');
html.user = data.results;
const htmlOutput =
HtmlService.createHtmlOutput(html.evaluate().getContent())
.setWidth(250).setHeight(300);
SpreadsheetApp.getUi()
.showModelessDialog(htmlOutput,'Details');
sheetLogger('info '+data.results.first);
}else{
SpreadsheetApp.getUi().alert('Sorry no Data!');
}
}
function checkSel(){
const ss = SpreadsheetApp.getActive();
const sheetName = ss.getActiveSheet().getSheetName();
if(sheetName == 'Users') {
const range = ss.getSelection().getActiveRange();
if(range.getRow() <= ss.getLastRow()){
return {
sName:sheetName,
results:getmyUser(range.getRow(),ss.getLastColumn())
}
}
}
return {
sName : sheetName,
results : null
}
}
function getmyUser(row,cols){
const vals =
SpreadsheetApp.getActive().getActiveSheet().getSheetValues(row,1,1,col
s)[0];
return {
id : vals[0],
first : vals[1],
last : vals[2],
email : vals[3],
row : row
}
}
function sheetLogger(message){
const id = '1xPf8w********IbIzBLkFw2rd7vuQ';
const ss = SpreadsheetApp.openById(id);
let sheetLog = ss.getSheetByName('log');
if(sheetLog == null){
sheetLog = ss.insertSheet();
sheetLog.setName('log');
sheetLog.appendRow(['Messages','Date']);
}
sheetLog.appendRow([message,new Date()]);
}

You might also like