Overloaded constructor error when creating trigger programmatically - google-apps-script

I want to create a process, when person submits a form and I get only filled in information (not every field is required for filling). Found code (http://www.labnol.org/internet/google-docs-email-form/20884/), did everything according to author, but doesn't work me. I'm not a specialist in coding, so I need your help.
Here is the code:
function Initialize() {
var triggers = ScriptApp.getScriptTriggers();
for(var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendGoogleForm")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendGoogleForm(e)
{
try
{
// You may replace this with another email address
var email = "justbeincredible#gmail.com";
// Optional but change the following variable
// to have a custom subject for Google Form email notifications
var subject = "New Driver Application Arrived";
var s = SpreadsheetApp.getActiveSheet();
var columns = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
// Only include form fields that are not blank
for ( var keys in columns ) {
var key = columns[keys];
if ( e.namedValues[key] && (e.namedValues[key] != "") ) {
message += key + ' :: '+ e.namedValues[key] + "\n\n";
}
}
// This is the MailApp service of Google Apps Script
// that sends the email. You can also use GmailApp for HTML Mail.
MailApp.sendEmail(email, subject, message);
} catch (e) {
Logger.log(e.toString());
}
}
Here is what I get when I run Initialize to test:
Cannot call overloaded constructor forSpreadsheet with parameters
(null) because there is more than one matching constructor signature:
interface SpreadsheetTriggerBuilder forSpreadsheet(Spreadsheet)
interface SpreadsheetTriggerBuilder forSpreadsheet(String) (line 12,
file "Code")

Add the code inside the Script Editor of the Google Spreadsheet that is collecting the responses and not inside the Form editor.
[source]
Explanation: The error message indicates that forSpreadsheet() had a parameter of (null). You fed it SpreadsheetApp.getActiveSpreadsheet(), so that evaluated to null. That method is only applicable within Spreadsheet-bound scripts.

The add-on doesn't work on brave browser. Try running on Google Chrome if you are not currently using it.

Related

Issue running an Installed Trigger in Google App Script

Fairly new to app script so bare with me.
Wrote this massive script, then went to set it up on a times trigger and it just refuses to run. I've gone ahead an back tracked as much as I could, to get at least something to work, yet I can't even get a basic toast to appear on a minute interval.
This is the script I've built, which I'm running directly to enable the trigger:
function createTriggers() {
ScriptApp.newTrigger('testTime')
.timeBased()
.everyMinutes(1)
.create();
};
The function it's calling is super simple, I've used it a lot and change it a lot too:
var gSS = SpreadsheetApp.openById("this_1s/the.1d")
function testTime() {
var d = new Date()
var Start = d.getTime();
gSS.toast(Start, "Testing", 30)
};
So how it should work, and it does if I just call the 'testTime' function directly, is a little toast pop-up appears on the spreadsheet in question, and stays visible for 30s.
When I run the trigger function 'createTriggers', nothing happens..
Please help! All the code I wrote is for nothing if I can't get it to run on its own.. :(
***** EDIT - 08/04/20 - based on comments *****
It's possible this was an XY example, I tried to run a small segment of the original code which works when I run it directly, and its not working here either.. this snippit does not have any UI facing functions in it, so it shouldn't be the issue..
All i did was take the above trigger function and change the name to 'testClear', which calls to the following functions:
function testClear(){
sheetVars(1)
clearSheetData(sheetSPChange)
};
function sheetVars(numSprints) {
// returns the global vars for this script
try {
sheetNameSprints = "Name of Sprint Sheet"
sheetNameSPChange = "Name of Story Point Change Sheet"
sheetSprints = gSS.getSheetByName(sheetNameSprints)
sheetSPChange = gSS.getSheetByName(sheetNameSPChange)
arraySprints = iterateColumn(sheetSprints,"sprintIDSorted", 1, numSprints)
}
catch(err) {
Logger.log(err)
};
};
function iterateColumn(sheet, header, columnNum, numRows) {
// Create an array of first column values to iterate through
// numRows is an int, except for the string "all"
var gData = sheet.getDataRange();
var gVals = gData.getValues();
var gLastR = ""
var gArray = []
// check how many rows to iterate
if (numRows == "all") {
gLastR = gData.getLastRow();
}
else {
gLastR = numRows
};
// Iterate through each row of columnNum column
for (i = 1; i < gLastR; i++){
// iterate through
if(gVals[i][columnNum] !== "" && gVals[i][columnNum] !== header){
// push to array
gArray.push(gVals[i][columnNum]);
}
else if (gVals[i][columnNum] == "") {
break
};
};
return gArray
};
function clearSheetData(sheet) {
// Delete all rows with data in them in a sheet
try {
if (!sheet.getRange(sheet.getLastRow(),1).isBlank()){
sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn()-1).clearContent()
Logger.log("Sheet cleared from old data.")
}
else {
sheet.deleteRows(2, sheet.getLastRow()-1)
Logger.log("Sheet rows deleted from old data.")
};
}
catch(err){
Logger.log(err)
emailLogs()
};
};
The 'emailLogs' function is a basic MailApp so i get notified of an issue with the script:
function emailLogs() {
// Email Nikita the loggs of the script on error
var email = "my work email#jobbie"
var subject = "Error in Sheet: " + gSS.getName()
var message = Logger.getLog()
MailApp.sendEmail(email, subject, message)
};
Thanks to a comment I've now discovered the executions page!! :D This was the error for the edited script.
Aug 4, 2020, 10:48:18 AM Error Exception: Cannot call
SpreadsheetApp.getUi() from this context.
at unknown function
To show a toast every certain "time" (every n iterations) add this to the for loop
if (!((i+1) % n)) spreadsheet.toast("Working...")
From the question
Aug 4, 2020, 10:48:18 AM Error Exception: Cannot call SpreadsheetApp.getUi() from this context. at unknown function
The above error means that your time-drive script is calling a method that can only be executed when a user has opened the spreadsheet in the web browser.
In other words, toast can't be used in a time-driven trigger. The solution is to use client-side code to show that message every minute. To do this you could use a sidebar and a recursive function that executes a setTimeout
References
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Timeouts_and_intervals
Based on all the comments, and new things I'd learned from that..:
I'd been calling to a global variable for my onOpen function:
var gUI = SpreadsheetApp.getUi();
even though I wasn't using it for the trigger, since it was global it tried to run and then failed.
I moved the actual definition of gUI into the onOpen function, and tried again and it worked.
Thank you all for the support!

Get the ID of a spreadsheet

So, I'm already trying this for a week, still errors. Can get the spreadsheet ID properly.
Currently I have this code:
function getSS(e,getSS) {
//If not yet authorized - get current spreadsheet
if (e && e.authMode != ScriptApp.AuthMode.FULL) {
var getSS = SpreadsheetApp.getActiveSpreadsheet();
}
//Else if authorized set/get property to open spreadsheet by ID for time-driven triggers
else {
if(!PropertiesService.getDocumentProperties().getProperty('SOURCE_DATA_ID')){
PropertiesService.getDocumentProperties().setProperty('SOURCE_DATA_ID', e.source.getId());
}
var getSSid = PropertiesService.getDocumentProperties().getProperty('SOURCE_DATA_ID');
var getSS = SpreadsheetApp.openById(getSSid);
}
return getSS;
}
var SS = getSS();
It's supposed to get active spreadsheet ID when the addon is not yet authorized, and get a spreadsheet ID from properties when it's authorized. However, when testing as installed, I always get an error that I don't have permission to use openById() or getDocumentProperties()
How do I keep SS as global variable without it being null in any authMode?
Note that global variables are constructed each and every time that Apps Script project is loaded / used. Also note that no parameters are passed to functions automatically - you have to designate a function as either a simple trigger (special function name) or an installed trigger before Google will send it an argument, and in all other cases you have to explicitly specify the argument.
The problem is then that you declare var SS = getSS(); in global scope, and do not pass it any parameters (there are no parameters you could pass it, either). Thus in the definition of getSS(), even if you have it as function getSS(e) {, there is no input argument to bind to the variable e.
Therefore this criteria if (e && ...) is always false, because e is undefined, which means your else branch is always executed. In your else branch, you assume that you have permissions, and your test never was able to even try to check that. Hence, your errors. You might have meant to write if (!e || e.authMode !== ScriptApp.AuthMode.FULL) which is true if either of the criteria is true. Consider reviewing JavaScript Logical Operators.
While you don't share how your code uses this spreadsheet, I'm quite certain it doesn't need to be available as an evaluated global. Any place you use your SS variable, you could have simply used SpreadsheetApp.getActiveSpreadsheet() instead.
Your getSS() function additionally force the use of a permissive scope by using openById - you cannot use the preferred ...spreadsheets.currentonly scope.
Example add-on code:
function onInstall(e) {
const wb = e.source;
const docProps = PropertiesService.getDocumentProperties();
docProps.setProperty('SOURCE_DATA_ID', wb.getId());
/**
* set other document properties, create triggers, etc.
*/
// Call the normal open event handler with elevated permissions.
onOpen(e);
}
function onOpen(e) {
if (!e || e.authMode === ScriptApp.AuthMode.NONE) {
// No event object, or we have no permissions.
} else {
// We have both an event object and either LIMITED or FULL AuthMode.
}
}
Consider reviewing the Apps Script guide to add-on authorization and setup: https://developers.google.com/apps-script/add-ons/lifecycle
So I made it this way:
//Because onInstall() only runs once and user might want to launch addon in different spreadsheets I moved getting ID to onOpen(),
function onInstall (e) {
getAuthUrl();
onOpen(e);
}
Cases for different AuthModes.
function onOpen(e) {
var menu = SpreadsheetApp.getUi().createAddonMenu();
if (e && e.authMode === ScriptApp.AuthMode.NONE) {
menu.addItem('Authorize this add-on', 'auth');
}
else {
//If addon is authorized add menu with functions that required it. Also get the id of the current spreadsheet and save it into properties, for use in other functions.
menu.addItem('Run', 'run');
var ssid = SpreadsheetApp.getActive().getId();
var docProps = PropertiesService.getDocumentProperties();
docProps.setProperty('SOURCE_DATA_ID', ssid);
}
menu.addToUi();
}
Function that pops authorization window:
function getAuthUrl() {
var authInfo,msg;
authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);
msg = 'This addon needs authorization to work properly on this spreadsheet. Click ' +
'this url to authorize: <br><br>' +
'<a href="' + authInfo.getAuthorizationUrl() +
'" style="cursor:pointer;padding:5px;background: #4285f4;border:1px #000;text-align: center;margin-top: 15px;width: calc(100% - 10px);font-weight: 600;color: #fff">AUTHORIZE</a>' +
'<br><br> This spreadsheet needs to either ' +
'be authorized or re-authorized.';
//return msg;//Use this for testing
//ScriptApp.AuthMode.FULL is the auth mode to check for since no other authorization mode requires
//that users grant authorization
if (authInfo.getAuthorizationStatus() === ScriptApp.AuthorizationStatus.REQUIRED) {
return msg;
} else {
return "No Authorization needed";
};
console.info('Authorization window called');
}

Using data from var in subjectLine

I'm modifying Amit's code ( found here: http://labnol.org/?p=20884)
to try to send email with the data from a Google Form.
But what I'm trying to grab is from his keys and columns.
I want to specifically take the first 1 and 2 column's data from the row in question and use it as a var in the subject field.
But the output (in email and when sent to asana) is listed as undefined. Where did I go wrong?
/*
Send Google Form Data by Email v4.2
Written by Amit Agarwal amit#labnol.org
Source: http://labnol.org/?p=20884
*/
/**
* #OnlyCurrentDoc
*/
function Initialize() {
try {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers)
ScriptApp.deleteTrigger(triggers[i]);
ScriptApp.newTrigger("EmailGoogleFormData")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit().create();
} catch (error) {
throw new Error("Please add this code in the Google Spreadsheet");
}
}
function EmailGoogleFormData(e) {
if (!e) {
throw new Error("Please go the Run menu and choose Initialize");
}
try {
if (MailApp.getRemainingDailyQuota() > 0) {
// You may replace this with another email address
var email = "x+00000000#mail.asana.com";
// Enter your subject for Google Form email notifications
var key, entry,
message = "",
ss = SpreadsheetApp.getActiveSheet(),
cols = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// Iterate through the Form Fields
for (var keys in cols) {
key = cols[keys];
entry = e.namedValues[key] ? e.namedValues[key].toString() : "";
// Only include form fields that are not blank
if ((entry !== "") && (entry.replace(/,/g, "") !== ""))
message += key + ' :: ' + entry + "\n\n";
var first = entry[1];
var last = entry[2];
var subject = first+" "+last+": Interested Candidate";
}
MailApp.sendEmail(email, subject, message);
}
} catch (error) {
Logger.log(error.toString());
}
}
/* For support, contact developer at www.ctrlq.org */
entry is a string, defined here:
entry = e.namedValues[key] ? e.namedValues[key].toString() : "";
...which you later treat as an array:
var first = entry[1];
var last = entry[2];
At this point, first and last will both be undefined, because entry isn't an array. Further, this is inside a for loop that's traversing all the columns in the row - you can't see any bad side-effect from that, but these assignments and generation of a subject are happening multiple times.
That last clue suggests a better way to achieve your goal. Define the first and last variables before the loop, with default values. Then when looping over columns, watch for the columns containing the candidates' name, and update the default contents. Finally, after the loop, generate the subject line.
function EmailGoogleFormData(e) {
if (!e) {
throw new Error("Please go the Run menu and choose Initialize");
}
try {
if (MailApp.getRemainingDailyQuota() > 0) {
// You may replace this with another email address
var email = "x+00000000#mail.asana.com";
// Enter your subject for Google Form email notifications
var key, entry,
first = "unknown", last = "unknown",
message = "",
ss = SpreadsheetApp.getActiveSheet(),
cols = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// Iterate through the Form Fields
for (var keys in cols) {
key = cols[keys];
entry = e.namedValues[key] ? e.namedValues[key].toString() : "";
// Only include form fields that are not blank
if ((entry !== "") && (entry.replace(/,/g, "") !== ""))
message += key + ' :: ' + entry + "\n\n";
if (key == "first") { // Assumes "first" is column header
first = entry;
}
if (key == "last") { // Assumes "last" is column header
last= entry;
}
}
var subject = first+" "+last+": Interested Candidate";
MailApp.sendEmail(email, subject, message);
}
} catch (error) {
Logger.log(error.toString());
}
}
Sandy Good has created a similar app Data Director. I don't know why he did not mention it here? May be it's not what you're looking for.
I haven't used it yet, but thought his works might help someone who needs it.
----------------------------------------
OVERVIEW:
Send form data to different sheet. Integrate with Calendar. Sends emails. Makes an Edit URL and/or a PreFilled URL.
The Data Director for Forms Add-on has multiple features. It can send the form response to an alternate spreadsheet. It can send an email or multiple emails when the Form is submitted. It can add a guest to your calendar event.
When your Google Form is submitted, the Data Director for Forms Add-on can get the last form submission, and save it to a second spreadsheet destination of your choice. The destination spreadsheet can be any Google spreadsheet that your Google account has permission to write to. For example: Your Google Form currently writes data to a spreadsheet, but you want the form response to also go into a second sheet in the same spreadsheet. This Add-on can do that. Or the Add-on can write a copy of the form response to a completely different spreadsheet.
You should install this add-on if you want to save a copy the form response to to a destination other than what is set in the Form's design.
But that's not all Data Director can do! Data Director will also create an Edit URL and/or a PreFilled URL, and save those links to the spreadsheet.
There's even more! It will also send an email to the email address of your choice with a custom message. This is an extra option that you may want or need to use.
Here's a list of What Data Director can do!
Send a copy of the form response to a Google spreadsheet.
The same Google spreadsheet that is already receiving the Form response, or
A different spreadsheet than is currently receiving the Form response.
Exclude the timestamp from the copied response if you choose. The default is to include the timestamp.
Create an Edit URL and save a link to the destination spreadsheet.
Create a PreFilled URL and save the link to the destination spreadsheet.
Send multiple emails to the email addresses of your choice.
Send an email to the email address collected from a Form field.
Include the Edit Url and/or the PreFilled Url in the email.
CC the email to the address of your choice, or not.
Includes the option to specify the subject line.
The Body of the email can be written in the settings for the email. No need to create a template email.

Google Script not Finishing Successfully

I recently adapted a short piece of script to automatically send an e-mail to me when a specific response appears (the word FAIL) in a 'Google docs' spreadsheet, that collects responses from volunteers using a 'Google Form' to record their weekly checks on life-saving equipment.
The email on FAIL is working well, having tested it with a few FAIL responses. However, as the form 'owner' I am receiving notifications from Google, for each normal PASS response submitted, telling me that the script failed to finish successfully.
8/4/15 10:57 AM
SendGoogleForm
ReferenceError: "found" is not defined. (line 35, file "Code")
formSubmit
It finishes successfully if a FAIL response is submitted, so I suspect that, due to my inexperience with scripts, I have not defined what should happen when found is 'not true'.
It's probably glaringly obvious to a more experienced script writer but none of the things I have tried with 'Else' seem to work.
Any suggestions would be greatly appreciated.
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers)
ScriptApp.deleteTrigger(triggers[i]);
ScriptApp.newTrigger("SendGoogleForm")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit().create();
}
function SendGoogleForm(e) {
if (MailApp.getRemainingDailyQuota() < 1) return;
// Recipients email address
var email = "12345#abcde.co.uk";
// Subject for Google Form email notification
var subject = "Weekly Checks - FAIL Submitted";
var s = SpreadsheetApp.getActiveSheet();
var columns = s.getRange(1, 1, 1, s.getLastColumn()).getValues()[0];
var message = "A check form has been submitted containing a ** FAIL ** response: ";
// Look for a 'FAIL' response
var key = columns[keys];
for ( var keys in columns ) {
if ( e.namedValues[columns[keys]] == 'FAIL') {
found = true;
}
}
// Only include form fields that are not blank
if (found)
for (var keys in columns) {
var key = columns[keys];
if (e.namedValues[key] && (e.namedValues[key] !== "")) {
message += key + ' : ' + e.namedValues[key] + "\n\n";
}
}
MailApp.sendEmail(email, subject, message);
}
Your found variable is null whenever a non-"FAIL" occurs in your script and I think your if(found) is creating the error message.
You could Initialise your variable:
var found = false;
before you start testing it. That way, your if(found) will be false and it will skip down to your send email code.
Perhaps u can use
if ( e.namedValues["Pass or Fail"] == "Fail" ) { ... }
where "Pass or Fail" is the form question title. And also use e.range.getRow() to capture the sheet row that the responses were written.

How to pass parameters from one Google-Apps-Script to another and execute?

Goal is to pass data from Google Apps Script A to Google Apps Script
B.
Script A is published with execute as user permissions.
Script B is published with execute as me permissions (owner).
At the very least I want to be able to pass Session.getActiveUser.getEmail() from script A to script B.
This is what I have so far...
Script A
// Script-as-app template.
function doGet() {
var app = UiApp.createApplication();
var button = app.createButton('Click Me');
app.add(button);
var handler = app.createServerHandler('myClickHandler');
button.addClickHandler(handler);
return app;
}
function myClickHandler(e) {
var url = "https://script.google.com/macros/s/AKfycbzSD3eh_SDnbA4a7VCkctHoMGK8d94SAPV2IURR3pK7_MwLXIb4/exec";
var payload = {
name : "Gene",
activeUser : Session.getActiveUser().getEmail(),
time : new Date()
};
var params = {
method : "post",
payload : payload
}
Logger.log("Hello World!");
var HTTPResponse;
try{
HTTPResponse = UrlFetchApp.fetch(url, params);
}catch(e){
Logger.log(e);
}
return HTTPResponse;
}
Script B
function doPost(e){
if(typeof e === 'undefined')
return;
var app = UiApp.createApplication();
var panel = app.add(app.createVerticalPanel());
for(var i in e.parameter){
panel.add(app.createLabel(i + ' : ' + e.parameter[i]));
Logger.log(i + ' : ' + e.parameter[i]);
}
ScriptProperties.setProperty('Donkey', 'Kong');
return app;
}
output
Going to script A here the page loads the button. Clicking the button causes "Hello World!" to be logged in Script A's project log but the log of Script B's project remains empty.
TryCatch does not log any error.
I believe your problem is due that you try to pass as a response argument a uiapp element.
here a little variation of your script in html service.
the demo
the script:
// #### Part A
function doGet(e) {
var html ="<input type='text' id='text' /><input type='button' onclick='myClick()' value='submit'>"; // a text to be passed to script B
html+="<div id='output'></div>"; // a place to display script B answer
html+="<script>";
html+="function myClick(){google.script.run.withSuccessHandler(showResults).myClickHandler(document.getElementById('text').value);}"; // handler to do the job in script A
html+="function showResults(result){document.getElementById('output').innerHTML = result;}</script>"; // function to show the result of the urlfetch (response of script B)
return HtmlService.createHtmlOutput(html);
}
function myClickHandler(text) {
var url = ScriptApp.getService().getUrl();
var payload = {
name : "Gene",
text : text,
time : new Date()
};
var params = {
method : "post",
payload : payload
}
Logger.log("text: "+text);
var HTTPResponse;
try{
HTTPResponse = UrlFetchApp.fetch(url, params);
}catch(e){
Logger.log(e);
}
return HTTPResponse.getContentText();
}
// ###### Part B
function doPost(e){
if(typeof e === 'undefined'){
return "e was empty!!";
}
var htmlOut="<ul>";
for(var i in e.parameter){
htmlOut+="<li>"+i+ " : " + e.parameter[i]+"</li>";
if(i=="text"){
htmlOut+="<li> Text hash : "+Utilities.base64Encode(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, e.parameter[i]))+"</li>";
}
}
htmlOut+="</ul>";
return ContentService.createTextOutput(htmlOut);
}
It is important to note that you won't have the ability to get the logger events of the script B (because it is triggered when you are not there - you are not the one who trigger script B. this is script A that's trigger script B and script A is not identified as "you" when it make a urlfetch). If you want to get the result of the script b logger you should return it to the script a.
It's important to note: again, when script A do the UrlFetch to script B it is not identified as "you" so the script B must accept to be opened by anyone (in the publish option under "Who has access to the app:" you need to select anyone even anonymous).
NB: i put everything in the same script for commodity (you can split that in two differents script it's not a problem) and because B part need to be accessed to anonymous persons I can't retrieve automatically the email adress in the part A so I changed a little bit what was done here.