Send email programmatically - javascript

I wrote a custom function called sendEmails in a script linked to a google spreadsheet. It works when I run the function through the script editor, however, it does not work when I run it through the spreadsheet.
My goal is to let the spreadsheet run without having a user and send an alert (in my case an email) automatically when a condition is met on the spreadsheet's cells.
This is the not working code:
=if(V3=X3,sendEmails(),"Waiting")
The alert can be anything that I can read using my phone or another computer.

With custom functions you are limited by the kinds of services that you can use because custom functions never ask for authentication. https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services
You could add your function to the add-on toolbar or setup an installable trigger to run the sendEmails() function.
Here is some more documentation to help you out.
https://developers.google.com/apps-script/guides/menus
https://developers.google.com/apps-script/guides/triggers/installable

Without a look at the full list of code, it's difficult to say. Two possibilities based on your single line of code.
1) It's not enough to say =if(V3==X3) and assume that if they become equal, they will trigger. You must specify for the spreadsheet when it should check if these are equal (For example, on spreadsheet edit, or at a specified time interval).
2) In your if statement, you are specifying that V3 SHOULD equal X3, not checking if it is. =if(V3==X3) or =if(V3===X3) resolves this.

Related

Google Apps Script Multiple users using same functions and javascript issues

I am looking for some advice on what to read up on. I am trying to make a webapp for my school that teachers can user for the school reward system. It is designed as a front end to a google sheets spreadsheet. The teachers log how many housepoints a child has earned in a lesson via a table with a column of text inputs (one for each child in the class) The webapp then adds these to the spreadsheet using the appendrow method.
I have one problem currently and need some advice on what I should do about another issue I can see on the horizon!
Current issue:
I have a javascript function that checks the housepoint values that have been added to each child collects a list of ids and the number of housepoints and sends these to google apps script as attributes of an object via a for loop.The apps script then strips the attributes and adds the data to a spreadsheet. The issue is as I understand it the apps script takes too long to append the rows and the javascript for loop does not wait for the apps script to catch up. The result is that it misses some of the data out and only appends a fraction of the rows it should do. Is there a way to get the javascript function to pause and wait for the app script to say its ready for the next object?
Potential issue: As this script is meant to be used by multiple teachers at the same time I am worried that if the same function (to add the housepoints to the spreadsheet) is called at the same time by different users what will the app script do? I would imagine it will collapse. I have tried to investigate what to do on this and have to say I do not understand what I am reading. What I really want is the app script to "park" each request in a cue and then chug through them at its own speed. Is this possible?
As you may have guessed I am not an experienced programmer, I am willing to read up and learn but just would like someone to point me in the right direction. Many thanks in advance for your time.

jquery.cardswipe plugin demo callback to success function not working with card reader

Trying out Carl Raymond's jquery.cardswipe plugin, but having difficulties with the demo-simple.html. jquery.cardswipe
It is configured to do a callback to a success() function that simply writes the resulting data set (parsed from the input of a magnetic stripe card reader) and display it on the webpage. I've verified the card reader by itself is reading data successfully from some custom ID badges into Notepad. When using the demo-simple.html example with the debug parameter set, the console displays the array of chars that were read in from the card (which indicates the plugin is getting data). BUT the success() function never gets called and never generates the display text it should. To troubleshoot it, I added a console.log in the success function, but it never gets called. I've tried it with the generic parser, as well as a custom one that I based off the help example.
Anyone that has experience with this plugin have ideas of what I'm missing?
After looking closer at the jquery.cardswipe.js code and checking the console in debug mode, I found two mistakes I had made.
First: This plugin expect the card data to be encoded to contain a letter for the second character (mine had a digit).
Second: The RegExp expression syntax used in my custom parser was incorrect.
Hopefully, this may help others that are new to using card scanners with custom magnetic stripe encoding.

Creating reusable function in Google AdWords

I'm currently writing a script in Google AdWords, and I'm managing lots of campaigns that use the same function (which I have had to copy-paste around 90+ times). Whenever I want to edit this function it becomes incredibly hard as I need to manually to through every script individually.
Is there any way of creating a reusable function, store it as a separate script and call it from AdWords like how I can call Logger and DriveApp? I've tried to use the following resources, which has been pretty useless so far:
Standalone Scripts - Google somehow allows you to create scripts but not execute them from within AdWords (at least that's as far as I've managed to come)
Execution API - Gave up after getting constant 'Unauthenticated' errors (even when setting the permissions to 'Anyone, even anonymous')
MCC Scripts - This allows me to update all accounts, but doesn't do what I need
If anyone knows any way of simply storing a function somewhere and calling it within Google AdWords, it would be greatly appreciated.
You can write your function a save it in a Google Drive file and the use it from there:
var scriptFile = getFile(location);
var scriptText = scriptFile.getBlob().getDataAsString();
eval(scriptText);

JS Page Searcher

I need to bruteforce through pages and check it's contents for certain text.
ie. I have page: http://example.whatever/internet?ip=$4.2$$.$$6.$$$ and those dollar signs, are missing numbers. I need to go through all of existing combinations untill 404 - Page not found text isn't present on that page anymore (it's not an error code, that gets returned, just the text.)
I also need to be able to use it when there is login required to access the pages, and I have the necessary access, that's why I suggest JS to be used in this, and not server side script, like PHP or Python.
Edit 1: In case you want to complain, that this is not a question, I'm asking how to do it. And if you don't have the balls (or whatever it is you have or don't have) to comment bellow, why you did press that arrow down next to this question, so we can resolve it, please don't do it. Thanks for understanding.
I'd recommend using something like PhantomJS for this. You could login then iterate through pages until you find a non-404 page.

How to synchronize Google Calendar & Spreadsheet with Script

I am trying to create a Google Apps Script that keeps a Google Calendar and a "master spreadsheet" on Drive synchronized -- is this possible? I found these two posts:
http://blog.ouseful.info/2010/03/04/maintaining-google-calendars-from-a-google-spreadsheet/
http://blog.ouseful.info/2010/03/05/grabbing-google-calendar-event-details-into-a-spreadsheet/
I'm quite sure this could be done using a lot of if statements and logic, but maybe there's a simpler way?
I ended up just providing the following simple script. All that was really necessary was adding events based on two columns, and this would've taken too long to develop.
function onOpen() {
//spawns a menu with a button that triggers AddToCal
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Add event to calendar",
functionName : "AddToCal"
}];
sheet.addMenu("Data To Calendar Plugin", entries);
};
function AddToCal(){
//get the current row
var ss = SpreadsheetApp.getActiveSpreadsheet();
var cell = ss.getActiveCell();
var R = cell.getRow();
//grab values for current row to pass to calendar event
var date_of_event = ss.getRange('G'+R).getValue();
var date = new Date(date_of_event);
var event_title = ss.getRange('A'+R).getValue();
//access the calendar
var cal = CalendarApp.getCalendarById('[IDREMOVED]');
cal.createAllDayEvent(event_title,date);
ss.toast("Event added to " + cal.getName());
}
Yes, it's possible to write a two-way event synchronization script, but it isn't going to be simple. Those two posts you refer have parts that could be reused, but they are quite elementary compared to the challenges you'll face with actual synchronization. You may want to read over Using Google Apps Script for a event booking system which does create calendar entries based on a spreadsheet (but doesn't do on-going synchronization). I've done some debugging of that script in past.
Synchronization would need to support:
Creation of events in either location
Modification of event details in either location (although you could opt to consider only a subset of event details for simplification)
Deletion of events in either location
Recurrence, e.g. CalendarEvent.getEventSeries() handling (or choose to avoid)
This is pseudo-code that you could start with:
Open Calendar, Read Calendar events into calArray (will all attributes you care for)
Open Spreadsheet, Read Spreadsheet events into sheetArray
For each event in calArray:
Search for calEvent in sheetArray.
If found, compare lastUpdated values.
If equal, do nothing
Otherwise copy most recently updated to least recently updated
Continue with next event
If not found then copy calEvent to new sheetEvent, including lastUpdated value.
Continue with next event
For each event in the sheetArray (...that hasn't been handled yet)
Similar logic above.
Write updated sheetArray to spreadsheet.
Write updated calEvents to calendar API (see note 1 below)
Notes:
All updates to calEvents could be made to array and written to calendar API immediately, as an alternative to a bulk update. This would eliminate the need to track the changes locally, although it would be a good idea to touch the lastUpdated value.
You will want to use CalendarEvent.getLastUpdated() when reading calEvents, and store a similar value in your spreadsheet (tied to an onEdit trigger) to facilitate comparisons.
It would simplify comparisons to record CalendarEvent.getId() against events in the spreadsheet. You also have CalendarEvent.setTag(key,value) that could be used to record custom metadata into the calendar, for instance to indicate events that originated or have been synchronized with your spreadsheet. (These tags are not accessible through the GCal UI, so would only be accessible via script.)
You should think about the range of dates or number of events you want to deal with, and limit the scope of the script. If you don't, you are sure to run into execution time limits in real operation.
Some Calendar Event characteristics don't lend themselves to easy expression in a spreadsheet, for instance:
Guest list
Reminder list
As mentioned (thanks Henrique) in the other post I've spent some time - it was actually what brought me to GAS originally - on data exchange between spreadsheets and calendars mainly because people I worked with where used to organize their time schedule (for a highschool) in spreadsheets and I had to take care of the transition to Google Calendars.
After some time though it appeared that the online Calendar interface is far more effective to create events so they don't use the sheet to Calendar scripts anymore !!
On the other hand, the printing and presentation options in GCal are very limited so the other direction is still very useful and we use it all the time !
I know this will seem to be out of subject regarding the original question and maybe too anecdotic but I just wanted to point out that you should thoroughly think about what you really need before reinventing the wheel... As Mogsdad mentioned, some events parameter are not easily described in spreadsheet logic and finally it could become a lot more complicated to use than the original tool.
The only really useful tool I developed using bidirectional data transfer is a 'batch modification tool" when I need to delete or edit a big number of similar events.
For example if we need to change a teacher's name for some reason all along the year I import all the events for a number of class, replace the name in the spreadsheet and update back the class calendar... it takes me 5 minutes and is very easy but these are very specific use cases and I'm not sure it is very common.
Anyway I wouldn't call that "synchronization" since it only takes some events some time and changes them... I've never attempted to keep a spreadsheet up to date with a calendar, from my experience calendars are pretty reliable and I consider them as the original data source. As I already said, we import data in spreadsheets every week just for printing and local archiving.
Sorry for this long and a bit vague comment (that was way too long to fit in a normal 500 chrs comment ;-)
No there isn't. And although a lot of issues regarding Apps Script Calendar Service has been solved (timezones, all day events, queries and so on), it's still a fairly complex task.
I know Serge, the top contributor here in SO on #google-apps-script tag, has developed quite some scripts involving the Calendar Service.
But I don't know of anyway that made a two-way update between a calendar and a spreadsheet. It should be a hard one. If you ever do, please be kind to share :)
I wrote a script that synchronizes between GCalendar and a GSheet. You may be able to use it as is, or can certainly borrow ideas from it. There are separate commands for copying events each way: https://github.com/Davepar/gcalendarsync

Categories

Resources