I have a script that creates a new label in Gmail. Since it is a new label, it is not associated with any email message, yet. I would like to get the URL associated with the label using Apps Script. I have simimlar code that gets the URL for a folder.
I do:
var label = GmailApp.getUserLabelByName(labelName);
to get the label object. I was hoping something like:
label.getUrl()
would return the URL for this label. The script saves without error. It gets an error when I run it:
label.getUrl is not a function
Note that creating a folder object and then then doing:
newFolder.getUrl()
works fine.
Related
I am writing a Google App Script to parse and extract some content of some emails from Gmail. Unfortunately, I am having a quite hard time parsing the content of a message.
I am calling getPlainBody() on a GmailMessage to apply some parsing via a regex, however it doesn't seem to work properly. It looks like that the string returned by getPlainBody() is incomplete. Let me explain better.
When I apply the regex in this way it doesn't find anything.
function doGet(e) {
// code to get messages from GmailApp
const body = message.getPlainBody()
const result = body.match(/word/gm) // nothing found
const templ = HtmlService.createTemplateFromFile('messages');
templ.body = body
return templ.evaluate();
}
If I visit the Google Script App rendered by the code above, I copy the string value of body rendered via the template, I open the console on the Dev tools and I apply the exact same regex to the copied string, it's working.
Am I missing something? It looks like that the value of body is not "ready" when I try to access it with the match function.
After hours of debugging, I finally found the culprit: the debugger. The function getPlainBody() is working perfectly, it's the Google Script App debugger that is cleaning up the string returned by the function when showing it in the debug panel.
My body variable was full of \r and \n characters that were not showing on the debugging session. This explains why the regex was working fine
when tested on regex101: the string I was copying from the debugger wasn't the actual string assigned to body.
I have spotted the error when I have tried to console.log(body) and then noticed that the value was completely different from the one shown in the debugger.
I fixed the code with the following line:
const body = message.getPlainBody().replace(/\r?\n|\r/g, " ")
I'm using a stock Google Apps Script for Link Checker (https://developers.google.com/google-ads/scripts/docs/solutions/link-checker#source-code). I've copied the template spreadsheet to reference but I keep getting response "Error: Please specify a valid Spreadsheet URL."
The part of the code that seems to be the issue is the 3rd row below:
function validateAndGetSpreadsheet(spreadsheeturl) {
if (spreadsheeturl == 'YOUR_SPREADSHEET_URL') {
throw new Error('Please specify a valid Spreadsheet URL. You can find' +
' a link to a template in the associated guide for this script.');
I've input my URL to replace the 'YOUR_SPREADSHEET_URL' but am not sure what I'm supposed to input into the
'Please specify a valid Spreadsheet URL. You can find' +
' a link to a template in the associated guide for this script.'
I tried putting the my spreadsheet URL in there, and also I tried leaving it out, but neither works. I am a complete Google Scripts novice so any help would be gratefully received!
You need to have changed the URL parameter on here:
var CONFIG = {
SPREADSHEET_URL: 'YOUR_SPREADSHEET_URL',
My ultimate goal is to access the contents of a file uploaded via a Google Form from within a function triggered by formSubmit. I added some info in a comment to this question, but I think I need to update the question itself. When I deactivate the Smartsheets Sync add-on in the web form, this all works as expected. My theory is that the Smartsheets Sync add-on is not preserving the Event object in certain scenarios.
I began with:
function onFormSubmit (e) {
Logger.log (e);
}
I set up my trigger and tested a form submission, and in the log, I saw:
[<datetime>] {authMode=FULL, source=Form, response=FormResponse triggerUid=<id>}
as expected. I also explored the FormResponse object and verified that a valid Google Drive ID is in the response.
Next, I added a call to DriveApp.getFileById:
function onFormSubmit (e) {
Logger.log (e);
var responses = e.response.getItemResponses ();
var file = DriveApp.getFileById (responses [1].getResponse ());
Logger.log (file);
}
Resubmitting a form brings up a permission error with DriveApp. Not surprising, so I ran onFormSubmit directly from the script editor. It failed because it was invoked without an Event object, but it did invoke the dialog that allowed me to grant DriveApp permissions.
Now, when I submit a form, the Event object doesn't contain a FormResponse object. From the log:
[<datetime>] {authMode=FULL, source=Form, triggerUid=<id>}
So, does granting DriveApp permission somehow revoke permission to inspect the user's response? Alternatively, is there another way for me to use Google App Script to access a file uploaded via a Google Form?
The file ID is put into the file upload answer (response). The following code gets the answer to the file upload question, which is the file ID. Note that arrays are zero indexed, so the first question is at index zero. This code assumes that the file upload question is the very first question.
If this answers your question, you can mark it as correct by clicking the green arrow.
function onFormSubmit(e) {
var file,fileID,form,responseID,submittedResponse,uploadResponse;
responseID = e.response.getId();//The the ID of the current reponse
Logger.log('responseID: ' + responseID)
form = FormApp.getActiveForm();//Get the Form that this script is bound to
submittedResponse = form.getResponse(responseID);//Get the response that
//was just submitted
uploadResponse = submittedResponse.getItemResponses()[0];//This assumes
//that the very first question is the file upload
fileID = uploadResponse.getResponse();//Get the file ID of the file just uploaded
Logger.log('fileID: ' + fileID)
file = DriveApp.getFileById(fileID);//Get the file
Logger.log('file.getName(): ' + file.getName());//verify that this is
//the correct file - and that the code is working
}
I have started to learn Google App Scripting and was stuck at the first example using the Forms Api.
Following is my code:
function testFunction(){
var newClientForm = FormApp.openById('1f-676zsIuVpsQVSr1tAxttTs********');
var clientForm = FormApp.openByUrl('Url of the form in edit mode');
Logger.log('The form Title is ', newClientForm.getTitle());
Logger.log('The form Title is ', clientForm.getTitle());
}
Either way when I check the logs I only get the string message with blank values.
To check that my form id is correct I have tried deleted new characters in the Id and the script reports an error. But when the correct Id is mentioned the script does not work as expected.
Any inputs would be greatly appreciated.
I have spotted my mistake
The issues was with the Logger.log() method which i was incorrectly using. I should have mentioned as
Logger.log('The form Title is %s', newClientForm.getTitle());
I was missing the formatter '%s'
Is it possible to reference GmailApp in a my_custom_js.html file, as opposed to the Code.gs file?
The following works when used in Code.gs:
// BEGIN email
// define email recipients
var email_recipient = uploader_email;
// Email subject
var subject = "Form submitted"
// Email body
var body = my_html;
Logger.log(email_recipient);
// Send email
GmailApp.sendEmail(email_recipient, subject, body);
// END email
But it doesn't work when used in a function in my_custom_js.html.
Developer Tools > Console shows error:
Uncaught ReferenceError: Logger is not defined
Uncaught ReferenceError: GmailApp is not defined
The reason I want it to run in the custom script, is that it utilises:
// BEGIN handle form submit
function handleFormSubmit(formObject) {
google.script.run.withSuccessHandler(updateUrl).processForm(formObject);
}
// END handle form submit
And, if I understand that code correctly, updateUrl is only running after a successful operation - so I'd like to send the email from the updateUrl() function (ie after a successful operation).
Perhaps I need to use something like a scriptlet , but for use in js files?
Edit
I'm looking into:
https://developers.google.com/apps-script/guides/html/reference/run
And will see if I can pass through the values to a function defined in Code.gs using the following in my_custom_js.html:
google.script.run.sendNotificationEmail(arg1,arg2);
Edit
That last idea worked, but would appreciate any insight on the original question, thanks.
This week for the first time I have been using Google's realatively new Javascript Client API. Using this api you could trigger your Google apps script to run from any custom website and get a callback with the result from any methods you would normally use with in the appscript. It took a bit of fiddling to get it to work, including setting up things in API console, but it's so cool you can run and call and trigger any appscript from anywhere.