Recurring generic error message Google Apps Script - google-apps-script

EDIT: Not sure if this matters but I am using G Suite for Nonprofits.END EDIT:::
I keep getting this error message upon attempting to manually run this script for the first time:
We're sorry, a server error occurred. Please wait a bit and try again. (line 2, file "Code")).
I have saved it and authorized its permissions already. I am simply trying to execute the script to get all of the existing responses' edit URLs.
There are no triggers associated with this. The script is intended to get the edit response URLs from a Google Form. "Edit after submit" is enabled in the Form, and it is set to public in regards to who can complete the form (not that this should matter since I am the owner of the form).
I use this script in this exact form on many other projects with no issues at all. I have also tried a different script for this task that I know works, with the same error message returned. The error message always references the line of code with the form ID/URL. I have verified the form ID. Thanks for your help.
function assignEditUrls() {
var form = FormApp.openById('some id');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
var data = sheet.getDataRange().getValues();
var urlCol = 6;
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0] ?
urls[timestamps.indexOf(data[j][0].setMilliseconds(0))] : ''
]);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}

Thanks to Rubén, I was able to solve my problem. In my script I was using the incorrect Form ID. When running a script to obtain the edit response URLs from a Google Form you must view the edit form URL in the editing mode of your specific form. Extract the Form ID from this edit URL NOT the view URL or share URL etc. This is the only way your script will access the correct form with the correct permissions. Thanks again to Rubén and all others who assisted.

Related

getting Exception: Invalid ID error, when using getFileById

I am trying to use a script from a tutorial online that will put URLs in a google forms response sheet so I can go back and edit responses after they have been submitted.
The script comes from this guy https://www.youtube.com/watch?v=nqOE_FIMd_w
and his instructions are here: https://docs.google.com/document/d/1m9V_AHZdA24pUAR1xGxQNt_y3k7J9RKoSG5v_9oFvcU/edit
here is the script from the tutorial:
function assignEditUrls() {
var form = FormApp.openById('Your form ke goes here');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Your responses Google Sheet name goes here - The tab name, not the file name');
var data = sheet.getDataRange().getValues();
var urlCol = Column number where URLs get entered goes here;
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}
It seems all simple enough, but the script keeps giving me an error:
Invalid ID (line 2, file "getEditLinks")
Seems to be a problem with the document key.
I have already given the script permission and I have concentrated my attemtps to resolve this by grabbing different parts of this URL for the file. (I remember that Google used to need the whole URL at some point in the past)
Mine is
*https://docs.google.com/spreadsheets/d/1pKmad.....VtT3GaM/edit#gid=1905774080*
(where ..... is more of the doc key. I am not putting the whole lot for security reasons)
According to the tutorial, and all other research into this, it seems this part is the correct part from the URL to use.
1pKmad.....VtT3GaM
But this is what bring the error.
I tried the whole URL, the URL just up to the doc key part, and a few other subsets of this, but none work.
Can anyone see what I am doing wrong here?
Issue:
Based on your description you are trying to access a spreadsheet file by its id but you are using FormApp.
Instead you should be using SpreadsheetApp instead:
var form = SpreadsheetApp.openById('1pKmad.....VtT3GaM'); // form is now a spreadsheet object
but the new issue now is that the variable is not form anymore but a spreadsheet object and as a result you will get other errors down the road when calling form.getResponses().
Solution:
You need to use your existing script and put the id of the form instead of the spreadsheet, which can be found on the form url:
var form = FormApp.openById('Form ID here not Spreadsheet ID');
Note:
The form url looks like this:
https://docs.google.com/forms/d/formID/edit
and you can find the form that is attached to the spreadsheet file by the spreadsheet file menu:

How to find Google Form ID

Update! The Form ID is in the web address.
Someone kindly pointed out to me (outside of Stack Overflow) that the Form ID which identifies the form can be found in the web address. Example:
https://docs.google.com/forms/d/1y9XMjvZi_wlFC2FN64cdafIyGuM_7UOsn1Q61PXv5MQ/edit
Where the bolded text is the Form ID.
Original:
Apologies in advance, as I am not an experienced script writer and may use improper jargon.
But - I found the response to this question when googling about how to find the Form ID element in a Google Form, but the answer is old and my problem is similar. I have tried searching the "Inspect Element" data and still cannot identify the ID attribute. I've tried searching the code for:
data-item-id="##########", or
aria-describedby="i.desc.########## i3"
for="entry_XXXXXXX"
Still no luck. Does anyone have an updated suggestion for finding the Form ID? For reference, I am using the script below as my starting point. This is taken from a different/old form, but is similar in functionality so I was hoping to leverage it. I am trying to find my Form's ID (such as that as in line 6-7).
/**
* #NotOnlyCurrentDoc
* Limit OAuth scope to only current spreadsheet rather than all sheets in drive
*/
function onSubmit(e) {
var form = FormApp.openById('1eccBQ7Bs34Z54keGHeR8HDXmiP1UqxMi9vSDWNv6zgs');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Master');
var data = sheet.getDataRange().getValues();
//var urlCol = 26;
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
}
sheet.getRange(2, urlCol = 27, resultUrls.length).setValues(resultUrls);
return resultUrls;
};
As the OP mentioned in the question body, the Form ID could be found in the Google Form URL, but hte OP missed to mention that this URL is the edit URL, not the "view" URL.
The form owner also could find the form ID by using the web-browser developer tools by searching for data-redirect-url which has assigned the Form URL:
data-redirect-url="https://docs.google.com/forms/d/form_id/edit?usp=redirect_edit_m2"
As mentioned by the OP the Form ID is between https://docs.google.com/forms/d/and /edit.
Besides looking at the Form URL, the form ID could be retrieved by using Google Apps Script. The easier way is to use a bounded script similar to this:
function getActiveFormID(){
const id = FormApp.getActiveForm().getId();
console.log(id);
}
Another way is by using the Drive Service. The following example assumes that there is only one file named "My Form":
function getFormId(){
const id = DriveApp.searchFiles(`title contains "My Form"`).next().getId();
console.log(id);
}
From Google Chrome, open the form (view mode, not edit).
Hit F12.
In the Elements window pain,
expand <body>
expand <script type = "text/javascript" nonce> == $0
You should see:
var FB_PUBLIC_LOAD_DATA with each element you created on their form and their respective Element ID.

Google App Script to sync gmail inbox with sheets

I have a script pulling out data from a gmail account. The script scans the inbox for mails and finds the relevant lines of text and puts it in a Google sheet.
The email looks something like:
Vehicle: 5761364, Position: (URL to Google Maps)
The script i use to get the data to sheets is:
function processInboxToSheet() {
var start = 0;
var threads = GmailApp.getInboxThreads(start, 100);
var sheet = SpreadsheetApp.getActiveSheet();
var result = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
var content = messages[0].getPlainBody();
if (content) {
var tmp;
tmp = content.match(/Vehicle:\s*([A-Za-z0-9\s]+)(\r?\n)/);
var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No vehicle';
tmp = content.match(/Map Link:\s*([A-Za-z ][A-Za-z0-9!##$%?=^.,:&*/ ]+)/);
var comment = (tmp && tmp[1]) ? tmp[1] : 'No url';
sheet.appendRow([username, comment]);
Utilities.sleep(500);
}
}
};
Would it be possible to make a kind of synchronization function, where the Google sheet gets updated automatically with the emails in the inbox. Right now it makes duplicates every time it runs.
Also, could someone tell me if it is possible to get the script to delete the lines created if the email is deleted. So the sheet list always is in sync with the inbox?
Please ask me if it does not make sense.
To solve the issue of duplicate emails, you need to check whether a message is unread or not. If it is unread, then read it and make it as read afterwards. I have made video series on YouTube to explain how this is possible and published the full code on github. You may watch it here:
https://youtu.be/nI1OH3pAz6s?t=9
You can also get the full code from GitHub from the following link:
GitHub: Get Gmail Emails into Google Sheet
GitHub: Extract Body Contents from Gmail Emails
You could run your script from a timebased trigger and if you just rewrote the entire sheet each time then that would take care of eliminating the entries from deleted emails. If you could add the received date to the spreadsheet then you could order them by date.

How to generate a pre-filled form URL for Google Form

I'm looking for a programmatic way to automate the generation of pre-filled URLs for google forms.
In a recent update, Google introduced a new Forms product to Google Docs. If you open the tools menu in a spreadsheet, you'll see some new options.
In the new Form Responses menu, one option is "Get pre-filled URL". This opens up a dialog containing a version of your form that you can fill out, and when you submit it, you receive a URL that can be used to open a Live Form with the data you pre-filled ready and waiting for you. The URL looks something like this...
https://docs.google.com/forms/d/--Form-ID--/viewform?entry.1094330118=Something&entry.1471717973=stack#example.com&entry.540962741&entry.787941281&entry.1873343651
The questions from the form have a fixed identity, e.g. entry.1094330118. They may be pre-filled with a value (entry.1094330118=Something) or blank (entry.7879412).
In apps-script, I'd like to generate these pre-filled URLs for users of my form, so I can provide them for updates. My users are not members of an Apps Domain, so I don't have the option of embedding an Edit your response link.
If I can get the information about the form, I will be able to piece together the URL. While I can go through the UI to create one URL, and dissect it to get the info for that form, I want a solution that will work with arbitrary forms.
How can I programmatically determine the question IDs?
With the new
Forms product, is the form available to me through any apps-script
APIs? (I know about getFormURL() - that's not what I mean.)
Armed with a question ID, can I get more information about the question? (Question text, type, etc.)
I required something similar for users to go and edit their response, take a look here: http://productforums.google.com/forum/#!topic/docs/LSKKCR3VHC8
Copy / paste code below:
function assignEditUrls() {
var form = FormApp.openById('1MonO-uooYhARHsr0xxxxxxxxxxxxxxxxxxxxx');
//enter form ID here
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses');
//Change the sheet name as appropriate
var data = sheet.getDataRange().getValues();
var urlCol = 4; // column number where URL's should be populated; A = 1, B = 2 etc
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}
This is not possible right now but this request is being tracked on the Issue Tracker here. Please add your use cases there and watch it for updates.
I found no way to create a pre-filled URL from the form id itself. It is possible if the form has already an answer:
var form = FormApp.openById('1nGvvEzQHN1n-----_your_for_id_----zemoiYQA');
var responses = form.getResponses();
Logger.log(responses[0].toPrefilledUrl());
referring to this answer, it can be used to create prefilled urls, by replacing the last line like this:
instead of
FormResponse.submit();
it will be
FormResponse.toPrefilledUrl();

How to add "Edit Response" link to Google Forms emails?

I have a simple Google Form that collects data, and, using AppScript, sends confirmation emails to users who fill it out. After user submits the form, on confirmation, s/he will see a link to edit his/her response.
I'd like to include that link as a part of the confirmation email (Right now, it only shows up on the page.) How can I obtain the URL to edit a submitted response?
I am able to get the link to the Form through SpreadsheetApp.getActiveSpreadsheet().getFormUrl(). It gives me the following format: https://docs.google.com/a/domain.com/spreadsheet/viewform?formkey=<formKey>
The link however doesn't include the edit key, which is required for users to edit his/her response. The expected URL should look like this: https://docs.google.com/a/domain.com/spreadsheet/viewform?formkey=<formKey>&edit=<editKey>
Thanks for the help in advance!
-K
Edited:
Added a feature request on this: http://code.google.com/p/google-apps-script-issues/issues/detail?id=1345&thanks=1345&ts=1337773007
The answer that this wasn't possible by #Henrique Abreu was true until very recently. Google seems to have added getEditResponseUrl() to the FormResponse class and with that it becomes possible to use code like this to get the edit URL for a bunch of existing forms:
function responseURL() {
// Open a form by ID and log the responses to each question.
var form = FormApp.openById('1gJw1MbMKmOYE40Og1ek0cRgtdofguIrAB8KhmB0BYXY'); //this is the ID in the url of your live form
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
Logger.log(formResponse.getEditResponseUrl());
}
}
To make it automatically email the user as they respond one could add a trigger on form submit. As The situation I'm working with doesn't require people to log in with an apps account I don't have access to an email address automatically so I have a text question that captures the user's email address.
It does ask the question about whether or not editing the forms is what you want. I've been grappling with the relative advantages of editing an existing response or sending a prefilled form using toPrefilledUrl() so that I can see how things have changed over time. I guess this comes down to the value that tracking this will provide you.
If you are using Google Apps your responders can edit there form responses.
See: How to Edit Form Responses
--edit this is now possible. See other answers.
After user submits the form, on confirmation, s/he will see a link to
edit his/her response. I'd like to include that link as a part of the confirmation email
That is not possible, period.
That link is not accessible anywhere and one can't guess/construct it. But, there's some workarounds that might suit you (some suggested here that I'll re-phrase), e.g.
Send a per-populated form link and have the user re-send it. You'd need to have some kind of control field (e.g. the username), so you can know and delete/ignore his older submits. Possibly automatically via a script.
You could also develop and publish an apps-script GUI and send a link to this apps script plus a parameter that you generate where you can determine which entry you should edit. The down-side of this approach is that it's somewhat cumbersome and overkill to re-design the whole form on Apps Script. But again, it works.
At last, you could open an "Enhancement Request" on Apps Script issue tracker and wait until they and Google Spreadsheet/Forms team get together to develop a solution.
Here is a clear blog post that shows you how to do it step by step and explains what's going on under the hood for AppsScripts newbies:
http://securitasdato.blogspot.com/2014/11/sending-confirmation-emails-from-google.html
While collectively you can get there from the all the excellent answers provided here, the script from that post worked best for me.
Does this help - I haven't tried it but I was looking for the same thing a while ago and noticed this.
From this page
https://developers.google.com/apps-script/reference/forms/
code from there contains this:
Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());
Jon
Great, script works! Thanks.
For newbies, like me: Just paste the andre's code for function SendConfirmationMail(e) into your spreadsheet's code editor and set 'on form submit' trigger to run it. That's in spreadsheet script editor, not form script editor.
You need to hack in some values. Read the code. For me the confusing one was the need to replace the ********COLUMN SEQUENCE EX 14****** with the sheet column number where you want the edit urls to end up. I used 39 which is one column more than my form was using up.
However, I got runtime probs in this part:
for (var i in headers) {
value = e.namedValues[headers[i]].toString();
// Do not send the timestamp and blank fields
if ((i !== "0") && (value !== "")) {
message += headers[i] + ' :: ' + value + "<br>";
}
}
Dunno why, but I replaced it with this:
for (var keys in columns) {
var key = columns[keys];
if ( e.namedValues[key]) {
message += key + ' :: '+ e.namedValues[key] + "<br>";
}
}
Works for me.
Try This: (Credits is not for me, because i merge two solutions of the third part)
Source: Send Confirmation Email with Google Forms
/* Send Confirmation Email with Google Forms */
function Initialize() {
var triggers = ScriptApp.getScriptTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
var form = FormApp.openById('***YOUR FORM CODE***');
//enter form ID here
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('***SHEET NAME***');
//Change the sheet name as appropriate
var data = sheet.getDataRange().getValues();
var urlCol = ***************COLUMN SEQUENCE EX 14******; // column number where URL's should be populated; A = 1, B = 2 etc
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [], url;
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
url = resultUrls[i-1]
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
try {
var ss, cc, sendername, subject, headers;
var message, value, textbody, sender;
// This is your email address and you will be in the CC
cc = Session.getActiveUser().getEmail();
// This will show up as the sender's name
sendername = "****YOUR NAME******";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Registro de Oportunidade submetido com sucesso";
// This is the body of the auto-reply
message = "Nós recebemos seu registro de oportunidade.<br>Muito Obrigado!<br><br>";
ss = SpreadsheetApp.getActiveSheet();
headers = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["********COLUMN NAME OF DESTINATION E-MAIL************"].toString();
for (var i in headers) {
value = e.namedValues[headers[i]].toString();
// Do not send the timestamp and blank fields
if ((i !== "0") && (value !== "")) {
message += headers[i] + ' :: ' + value + "<br>";
}
}
message += "<br>Link to edit" + ' :: ' + url + "<br>";
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sender, subject, textbody,
{cc: cc, name: sendername, htmlBody: message});
} catch (e) {
Logger.log(e.toString());
}
}
you can try to populate a form with the values given from that email address than delete previous answers ...
it's not a beautiful way but it can works ...
I don't think we have access to what that value is through the Spreadsheet API (which means Apps Script doesn't have it either). The closest I can think of would be the "key" value in this feed. You'd have to test to find out though. There's no other alternative that I know of other than accessing the Spreadsheet API directly. So first, you'd have to get the last row through the api use ?reverse=true&max-results=1