I'm using Google Card Service to create a button, that when clicked should create a new Gmail draft; the draft should open for editing with a pre-populated body, and without a signature.
This is my code:
function onHomepage(e) {
var builder = CardService.newCardBuilder();
var section = CardService.newCardSection()
.addWidget(CardService.newTextButton()
.setText('Button')
.setComposeAction(CardService.newAction().setFunctionName('doStuff'),
CardService.ComposedEmailType.STANDALONE_DRAFT));
builder.addSection(section);
return builder.build();
}
function doStuff(e){
var body = 'This is a <b>test</b>'
var draft = GmailApp.createDraft('','Subject','',{htmlBody: body});
return CardService.newComposeActionResponseBuilder()
.setGmailDraft(draft)
.build();
}
The draft is created and opened for editing, the only issue is that the signature of the user is appended after the html body. I've seen other questions about adding a signature to the draft; I however need to remove it.
Unfortunately this isn't currently possible.
I would suggest filing a feature request on Google's Issue Tracker detailing the behaviour you would like to see, I would file it either under the Apps Script component or the Workspace Add-ons component.
Related
I'm running my gmail add-on with the following scopes:
https://mail.google.com/
https://www.googleapis.com/auth/gmail.addons.execute
https://www.googleapis.com/auth/gmail.addons.current.action.compose
https://www.googleapis.com/auth/gmail.addons.current.message.metadata
https://www.googleapis.com/auth/script.external_request
I have a button, and corresponding compose action event, that is supposed to read recipient, subject, and body information from some input fields and insert this info into the compose UI.
The compose action callback looks like this:
function autofillEmailDraft(e) {
var recipient = e.formInput.recipient;
var subject = e.formInput.subject;
var body = e.formInput.body;
GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken);
var draft = GmailApp.createDraft(recipient, subject, body);
// Return a built draft response. This causes Gmail to present a
// compose window to the user, pre-filled with the content specified
// above.
return CardService.newComposeActionResponseBuilder()
.setGmailDraft(draft).build();
}
On clicking the button, the addon crashes with the following error message: TypeError: Cannot read property "accessToken" from undefined.
e.messageMetadata seems to be undefined. Should I be looking for the access token somewhere else?
Simply removing the offending line
GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken);
doesn't work. The add-on crashes with a different error
Access denied: : Missing access token for authorization. Request: MailboxService.CreateDraft.
Update after comments:
This is the code for the button widget that triggers the compose action:
var submitButton = CardService.newTextButton()
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
.setText('Yes')
.setComposeAction(
CardService.newAction().setFunctionName('autofillEmailDraft'),
CardService.ComposedEmailType.STANDALONE_DRAFT
);
This is the logged 'e' object:
{
formInput={body=Hello},
parameters={},
draftMetadata={
toRecipients=[abcd#example.com],
bccRecipients=[],
ccRecipients=[]
}
}
As this open issue indicates, filling the recipient and subject fields of an email draft from an addon that extends the compose UI is not currently supported.
I am trying to create a document and set its access permissions.
The code below works on my normal Gmail account.
However, when I run it on my unique GSuite address, it returns this error:
TypeError: Cannot find function setSharing in object Document. (line
6, file "Code")
Here is the code:
function createAndSendDocument() {
//Create a new Google Doc named 'Hello, world!'
var doc = DocumentApp.create('Hello World');
//Set user permissions to view and edit.
doc.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
//Access the body of the document, then add a Paragraph.
doc.getBody().appendParagraph('This is a test document for people to see and try and lorem ipsum.');
//Get the URL of the doc
var url = doc.getUrl();
//Get email address of the active user - aka you.
var email = 'example#domain.com';
//Get the name of doc to use as an email subject line
var subject = doc.getName();
//Add a new strong to the url variable to use as an email body.
var body = 'Link to your document: ' + url;
//Send yourself an email with a link to the document.
GmailApp.sendEmail(email, subject, body);
}
How can I work around this or use the setSharing method correctly? Am I using the wrong method or am I missing other steps as
I had this problem from a non GSuite account and solved it by using the Drive api instead of the Document one.
e.g.
var drivedoc = DriveApp.getFileById(documentId);
drivedoc.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
worked but it did not when I used DocumentApp.getFileById.
I had a similar problem myself that might help you. I used the DocumentApp api to create a document. Then I used the DriveApp api to set the sharing. Notice the the URLs you get from the same document are different. If you email the URL you get for the document from the DocumentApp with someone outside your G Suite organization, they will have to sign in to open the document even though the sharing is set to "Anyone with the link. No sign-in required." But if you email the URL that you get from DriveApp to the person outside your G Suite organization, they will be able to open the link easily, with no sign-in required. So I think your code might work better if you used DriveApp to set the sharing of the document you create. Then make sure that you email the URL you get from the DriveApp api instead of the URL you get from the DocumentApp api.
function myFunction() {
var doc = DocumentApp.create("Test Document");
Logger.log("This is the URL that I get with DocumentApp: " + doc.getUrl());
var documentFileID = doc.getId();
var myFileDriveApp = DriveApp.getFileById(documentFileID);
Logger.log("This is the drive app URL: " + myFileDriveApp.getUrl());
//Set the sharing on the file using DriveApp
myFileDriveApp.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
}
I want to custom the invitation email when I share a google form. I have looking up in Google Script documentation and I haven't found some method or class useful. How can I customize this invitation email?.
Normally this email looks like:
I want to add a corporative image in the footer and maybe some text.
There isn't a method to create your own customized message. Below I am going to propose an alternate solution.
You can create a function that sends a customized mail. Just create an HTML file with your customized HTML content.
// i called this share.html in the apps script project
<h1>This is a header 1</h1>
<p>This is a paragraph</p>
<img src="<<SOME IMAGE SRC HERE>>" />
Then back in your code.gs file create a function like below:
function shareForm() {
var html = HtmlService.createHtmlOutputFromFile('share').getContent();
var recipient = '<<EMAIL ADDRESS>>';
var subject = 'this is the subject';
var body = 'this will get overridden by the HTML service';
var options = {
htmlBody: html
};
MailApp.sendEmail(recipient, subject, body, options)
}
You can run this function from the script editor, or you can build a button to add into the add-ons drop down options.
I'm was trying to show html body of gmail message in modal dialog window but faced an error calling SpreadsheetApp.getUi() method.
...
var html = HtmlService.createHtmlOutput(threads[i].getMessages()[0].getBody());
SpreadsheetApp.getUi().showModalDialog(html, 'My add-on');
...
How I can call that dialog? Or may be there is better aproach to display message(some panel or sidebar)?
This prompt box is misleading, as the error must be elsewhere. The new version of Sheets not only does support the getUI method, I believe it's specifically designed for for the new version of sheets.
I've expanded on your previous question and tested this myself with the following code in the new version of sheets:
function getMail() {
var threads = GmailApp.getInboxThreads();
var messages = threads[0].getMessages()[0];
var raw = messages.getPlainBody();
return raw;
}
function dialogueBox(){
var raw = getMail();
var htmlOutput = HtmlService.createHtmlOutput(raw);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'For mk_yo');
}
and it displays the prompt with no issues, as shown here. Try creating a new sheet with a new script and run the code above. Additionally, you can try ensuring that the sheet that you're adding this to is definitely using the new version of sheets.
In relation to if there's a better approach by displaying a side panel, yes you can use custom sidebars in the new version of Google sheets(and only the new version), but as this still uses the 'getUI' method, this won't resolve your current error, as this is not where the issue lies.
I am trying to display a Google Visualization GeoMap on my website. I created the code in the Code Playground and saved it as an Apps Script in my website. The page goes through the load process but nothing is displayed.
Here is the code:
function doGet() {
var app = UiApp.createApplication();
function drawVisualization() {
var query = new google.visualization.Query(
'https://docs.google.com/a/mantisnetworks.co/spreadsheet/ccc?key=0AoNHsySj5NxGdGt0dmxva3ZPb3dLYVpVZ2Z4TThNbGc&usp=drive_web#gid=0');
query.send(handleQueryResponse);
}
function handleQueryResponse(response){
if(response.isError()){
alert('Error in Query:' + response.getMessage()+''+response.getDetailedMessage());
return;
}
var data = response.getDataTable();
var geochart = new google.visualization.GeoMap(document.getElementById('visualization'));
var options = {};
options['dataMode'] = 'regions';
options['resolution'] = 'provinces';
options['region'] = 'LS';
options['width'] = '600px';
options['height'] = '300px';
geochart.draw(data, options);
}
app.close();
return app;
}
Google Apps Script is based on Javascript, but as a server-side environment it does not have access to all client-side javascript constructs. The Google visualizations, for instance, are provided as the Charts Service. Using that service, you'll find support for much of the visualization API. However, you won't find GeoMap.
The code you've provided in your question needs to be reworked considerably to work properly in Google Apps Script. Start with the example given on the Charts Service page, then adapt to your situation.
You do have another alternative within Google Apps Script, which is to use the HTML service to "host" an HTML page containing "real" javascript. Javascript that's embedded in HTML pages can be made to run on the client browser, so the example you cooked up in the playground should work. A full run-down of this option is beyond the scope of your question, but if you're interested in it you could start by scanning previous questions about the HTML Service.