r/GoogleAppsScript • • Sep 14 '20

Question Need help with an email script!

6 Upvotes

Hello all!

This is my first time putting together a script and I've been able to do as much as my knowledge and Google searching has allowed. If anyone can help, I would greatly appreciate it!

The goal of this script is use a Google sheet and automatically send emails to students reminding them of the assignments they are missing. I have been able to set up the email template and enable cc'ing the email to parents, but I can't figure out a few other things. Here is a sample sheet.

function sendEmail() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = sheet.getSheetByName('Sheet1');
  var sheet2 = sheet.getSheetByName('Sheet2');
  var numColumns = sheet1.getLastColumn();

  var subject = sheet2.getRange(2,1).getValue();
  var n = sheet1.getLastRow();
  for (var i = 2; i < n+1 ; i++ ) {
    var emailAddress = sheet1.getRange(i,3).getValue();
    var name = sheet1.getRange(i,1).getValue();
    var assignment = sheet1.getRange(i,5).getValue();
    var message = sheet2.getRange(2,2).getValue();
    var parentAddress = sheet1.getRange(i,4).getValue();

    message=message.replace("<name>",name).replace("<assignment 1>",assignment);
    
    MailApp.sendEmail(emailAddress, subject, message,
                      {cc: parentAddress});
  }
}

There are the 3 things I need help with:

  1. The contents of the cells under the different assignments are irrelevant; I only need to indicate if something is completed or not. In my template email, Assignment 1 gets replaced with X, since it is the contents of the cell. In this case, the X denotes the assignment is completed. What I really want is to check if a cell is empty, and then the assignment/column name will be added into the body of the email with any other assignments that have no contents in the appropriate cell.

  2. How can I set a condition where if a student has every assignment completed (none of the cells are empty), then an email will not be sent?

  3. How can the number of columns/assignments be dynamic? I want to be able to add new assignments/columns without jeopardizing the script. I've included getLastColumn(); but I am not sure how to actually use it.

Thank you!


r/GoogleAppsScript • • Jul 12 '20

Question Can someone explain now callback URLs work? Whats the difference between /exec and /usercallback?

6 Upvotes

Comment removed in protest of Reddit's API changes forcing third-party apps to shut down


r/GoogleAppsScript • • May 17 '20

Resolved How to active sheet?!?!?

6 Upvotes

I'm trying to figure out I don't know what or how.

Can anyone help me?

I am trying to do nothing. Just put text in an Sheet range.

I can't even figure out how to get the activeSheet.

Please kill me!

function learnBasics() {

var app = SpreadsheetApp;

var ss = app.getActiveSpreadsheet();

var activeSheet = ss.getActiveSheet();

// activeSheet.getRange("D2:F9").setValue("OMG"); //This worked fine so I moved on

// activeSheet.getRange(6, 2).setValue("Serious Stuff"); // This worked fine, moved on

//activeSheet.getRange(2, 2, 3, 3).setValue("Serious Stuff") //Chose Sheet2 expecting it to become the new ss but it writes to Sheet1

logger.log(ss) //me in absolute despair trying to see if I can figure the GD active sheet.

}

(a) How do I find out what apps script is going to treat as the activeSheet and (b) do I have any say over it?!?!? Can I specify which sheet I want to be the sheet that something happens to?!?!?


r/GoogleAppsScript • • May 05 '20

Question Create a CRUD system for my work management

6 Upvotes

Hey folks,

I just stumbled upon a way in which I can create custom webpages using google app script that can read and write to my google sheet. I wanted to create a full-blown CRUD website with which I can add new records, fetch records, delete records and edit records in my google sheet using a web app created in google app script. But I am not a programmer. I know basic programming but I don't know the complete thing. So can someone help me out with this? Or is there a way in which I can achieve this with lesser coding?


r/GoogleAppsScript • • Apr 05 '20

Guide Who knows any funny pranks you can execute in a Google sheet via script to play on co-workers?

6 Upvotes

Bit of a fun one but just wondering if anyone has any funny pranks they've written in script to wind up co workers etc?

A few months back my boss hijacked my laptop and wrote some VBA in Excel to put me in an infinite loop with a message box that read "Haha" that I couldn't get rid of, thoroughly hilarious for everyone in the room at the time (took me about 15 minutes to figure out how to get out of it)

He's great with VBA but not Apps Script so I've decided to try and get one over on him as a laugh through these crazy and extremely busy times at work!

Any suggestions?


r/GoogleAppsScript • • Mar 11 '20

Resolved Trying to send an email when a row is added (Google Sheets)

6 Upvotes

I'm trying to add a script to Sheets that will automatically send out an email with information from a new row that has been added. I've been looking into different options to do this, but I haven't had much luck. It doesn't help that I am very new to writing scripts. If anybody has a good resource to point me to for my specific application, I would love to hear it!


r/GoogleAppsScript • • Jan 31 '20

Create an ongoing script to automatically copy certain events from one Google calendar to another Google calendar

6 Upvotes

Hi,

I am trying to sync my work Google calendar to my personal Google calendar. I'd like to be able to automatically copy all work events EXCEPT for certain work events that start with a certain title (i.e., 'XYZ-') on an ongoing basis. For example, I have work events that all start with 'XYZ-'. The script would NOT copy these events to my personal Google calendar. Any help is appreciated. Thanks so much, will award coins! :)


r/GoogleAppsScript • • Dec 16 '19

Add hyperlink to a cell value without using the =HYPERLINK() formula?

6 Upvotes

Comment removed in protest of Reddit's API changes forcing third-party apps to shut down


r/GoogleAppsScript • • Dec 13 '19

I need help with my script for spreadsheets

6 Upvotes

hello, i am really new to this and i need help.

I have done this so far and it worked! however i want the name of the new spreadsheet to correspond with what i have on column c and line 2, then the second copy 3 and so on, what can i do?

function myFunction()

{

var ss = SpreadsheetApp.getActiveSpreadsheet();

ss.setActiveSheet(ss.getSheetByName("otro"));

var sheet1 = SpreadsheetApp.getActiveSheet();

var dataRange = sheet1.getRange("A1:C5");

var data = dataRange.getValues();

for(i = 0; i < 6; ++i)

{

var rowData = data[i];

ss.duplicateActiveSheet()

}

}

function onOpen()

{

var ss2 = SpreadsheetApp.getActive();

var menuA = [{name: 'create Sheets', functionName:'myFunction'}];

ss2.addMenu('mis herramientas', menuA)

}


r/GoogleAppsScript • • Nov 18 '19

Using Google Script to Input Google Drive Files' URLs into Google Sheets

6 Upvotes

Sorry in advance, but I think I should start by saying I'm not a dev nor do I have any dev knowledge. And I'm really hoping you could help me with this.

Scenario:

  • I have a folder in My Drive with several PDFs

  • I have a Google sheet with 2 columns, first column is "Name" and second column is "Document". The first column is filled with several names and the second column is entirely empty

What i want to do:

  • Import the URLs/links of the PDFs into the "Document" columns corresponding to the names. So John's PDF document should be in the same row with John's name

Someone from StackOverflow helped me come up with this script but it doesn't work:

function main () {   
    files = DriveApp.getFilesByType("application/pdf"); 
    var names = []; 
    var links = []; 

    while (files.hasNext()) { 
        var file = files.next();     
        names.push(file.getName());     
        links.push(file.getUrl()); 
} 
    var newSpreadsheet = SpreadsheetApp.create("My favourite pdf"); 
    Logger.log(newSpreadsheet.getUrl()); 
    var newSheet = newSpreadsheet.getSheets()[0]; 
    var a = newSheet.getRange("A1:A"+names.length ); 
    var b = newSheet.getRange("B1:B"+names.length );   
    a.setValues([names]);   
    b.setValues([links]); 
}

r/GoogleAppsScript • • Nov 04 '19

GAS-040 Gantt Chart

Thumbnail
youtu.be
6 Upvotes

r/GoogleAppsScript • • Oct 28 '19

Securely allow Google App Engine to internal company network/servers for Google Apps Scripts

6 Upvotes

It is well documented that Google Apps Script run on Google App Engine servers that would not have access to a company's internal network/server:

Per the documentation, if you want a Google Apps Script project to have access to an internal network/server then you will have to white-list Google's IPs. But we all know that isn't the safest option. In fact, the documentation even says so:

Note that using static IP address filtering is not considered a safe and effective means of protection. For example, an attacker could set up a malicious App Engine app which could share the same IP address range as your application. Instead, we suggest that you take a defense in depth approach using OAuth and Certs.

The issue is I cannot find any documentation, reference material, or articles on how best an organization should do what it suggests.

So my question is, how can an organization using G-Suite Enterprise securely allow Google Apps Script projects to access the company's internal network?


r/GoogleAppsScript • • Oct 17 '19

Writing in sheet and then exporting as PDF

6 Upvotes

Hello,

I've written a script which writes some data to a sheet and then exports it as pdf. I'm facing a peculiar behaviour.

sheet.getRange(1,1,tablelen,table[0].length).setValues(table);

This line is not actually setting values at that time, but after the whole script is complete. Because of this, my exported pdf is blank. I tried putting a Utilities.sleep(10000); after setValues line, that's when I discovered it wasn't setting values until the sleep was complete.


r/GoogleAppsScript • • Sep 01 '19

How can i group rows using google apps scripts ?

6 Upvotes

I am writing code in which a user can automatically generate a template of lesson and sub-topics. Each lesson will have 10 sub-topics.

I also need to group the rows lesson-wise and topic-wise. But, I am unable to group the rows lesson-wise and topic-wise. Tried using the macro-recorder, but the code does not work while generating multiple lessons.

Screenshot of the template Here is my code:

function Lessongeneration() {
      var spreadsheet = SpreadsheetApp.getActive();
      var sheet = spreadsheet.getActiveSheet();
      var row = 6;
      var col = 2;
    
      //Ask user for the no. of lessons
      var shlen = Browser.inputBox("Enter no of lessons", Browser.Buttons.OK_CANCEL);

  for (var i = 1; i <= shlen; i++) {

   sheet.getRange(row,col).setValue("Lesson " + i);
   row++; 

   // Add sub-topics (1.1, 1.2 ....)   
      leave 6 blank rows below each sub-topic. Then, group those blank rows

     for (var j=1;j<=10;j++){

       sheet.getRange(row,col).setValue(i+"."+j);

       **tried this, not working**
       sheet.getRange(spreadsheet.getCurrentCell().getRow() + 1, 1, 6, 
       sheet.getMaxColumns()).activate()
      .shiftRowGroupDepth(1);

       row=row+7;

     }
   }
};

r/GoogleAppsScript • • Aug 15 '19

Help With Calendar Sync From Spreadsheet

6 Upvotes

I am a complete novice at writing Google app scripts but have completed the following via some YouTube exploration:

function addEvents() {

var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

var lr = ss.getLastRow();

var cal = CalendarApp.getCalendarById("****");

var data = ss.getRange("B2:G" + lr).getValues();

var complete = "Done";

for(var i = 0;i<data.length;i++){

if (data[i][0] == "") {

break

}

cal.createEvent(data[i][0], data[i][3], data[i][4], {location: data[i][1], description: data[i][2]});

}

for(var i = 0;i<data.length;i++){

var dataRange = ss.getRange(2, 1, data.length, 7)

var data = dataRange.getValues();

if (data[i][5]!= 'ADDED_EVENT') { // Prevents adding duplicates

ss.getRange(2 + i, 7).setValue("ADDED_EVENT"); //Set status "ADDED"

}

}

}

Everything is working except I am having issues with events duplicating on the calendar and would like to prevent this. The script is currently triggered by a form submission and I'd like to use this to allow members of our organization to populate a calendar via the form.

Any help is greatly appreciated.


r/GoogleAppsScript • • Aug 13 '19

How to embed a Google Doc in an email via script?

6 Upvotes

I'm hoping someone can help me out with this. I've tried searching for a solution but have been unable to find one. What I'm trying to do is execute a script that will send an email. This email would send the contents of the Google Doc in the email. I don't want to attach the Google Doc nor do I want to link to the Google Doc.

I did find the following post on Stack Overflow, but some comments indicate the code no longer work.

https://stackoverflow.com/questions/13428246/google-app-script-attach-embed-document-into-email

Any help would really be appreciated.


r/GoogleAppsScript • • May 21 '19

Migration from employee to organisation

6 Upvotes

I think this is more of a gsuites admin question, but it still works here :)

I *know* my previous employer had some useful scripts in forms and documents and some other projects using api's (RPIs mostly) die because my gsuite account died after I left their employ.

I'm again writing useful scripts - how do I make them "not mine, my employer's"? obviously they have to accept ownership :/

Has this come up before please?


r/GoogleAppsScript • • May 10 '19

Send an automated email each week with the contents of a chart in google sheets

6 Upvotes

Hi guys,

only asking here as a last resort, sorry to bother you all!

As the title suggests, I'm trying to Send an automated email each week with the contents of a chart in google sheets.

Lets just say the email is [email protected], i'd need cells Z119:AA125 sent to the above email each week on a Friday. I think I know how to do the weekly trigger from past experience, so ive got that covered, but any help on the getting the chart into an email sent in a script would be so appreciated. If the formatting of the chart is included too, that would just be unbelievably good also! The tab name is 'Rota' fyi

any questions do let me know, thank you for reading my post! :)

best, Kw


r/GoogleAppsScript • • May 01 '19

Silent Auction Creation from Form

6 Upvotes

Use Google App Script to create Silent Auction forms for student artwork from input from google form. Form allows you to specify student name, artwork title, class, and photo of artwork. Script will create individual silent auction form to allow people to bid on pieces of artwork. Script also creates a google doc with photo, student name, class, and link to each auction form.

https://github.com/bekd70/SilentAuction


r/GoogleAppsScript • • Mar 18 '19

Send email automatically from spreadsheet

6 Upvotes

Hi, I am just figuring out about Google app script and would like some help with setting up a trigger that automatically sends and email notification once a cell in a column is edited. For example, Column B has the email addresses in it and Column K is going to be edited. I need to have an email sent to the email address in column B once column K gets edited. and the email has to say something along the lines of "your status has been updated". I was wondering if this was possible. Any help at all would be very appreciated.