Attach a Google Docs file in an email with Google App Scripts - google-apps-script

I'm trying to attach a Google Docs file to an email using Google App Scripts.
I have a folder with the file inside and the idea is to get the file from there and send it by email using also an email address that is saved in a google sheet.
I founded and used this:
function emailDocTestasDocx() {
var id = '1egjazIip6zuPneKh2sVFTvbK06asd4ZgAvrgjw';// an example of Google doc
var url = 'https://docs.google.com/document/d/1egjaasdasdeKh2sVFTvbK06z4JbPqnm4ZgAvrgjw';
var doc = UrlFetchApp.fetch(url);
var me = Session.getEffectiveUser().getEmail();
MailApp.sendEmail(me, 'test', 'see attachment', {attachments:[doc]});
}
It works but the final attachment is an html that redirects me to the doc inside the folder. I don't want that. I want to send the attachment but I didn't found a way to to this yet.
Any ideas? Thanks!

As what #Karan have mentioned on the comments, Google Docs are not something you can open outside of the cloud (it is always being opened via docs.google.com website via a link) so you cannot attach them as a file on the mail. You can either attach the document as PDF or export the Docs file first as a document with .docx extension & then save the file on your Google Drive:
Sample tweaked script that will send attach the Google doc file as PDF:
function emailDocTestasDocx() {
var id = '1egjazIip6zuPneKh2sVFTvbK06asd4ZgAvrgjw';// an example of Google doc
var url = 'https://docs.google.com/document/d/1egjaasdasdeKh2sVFTvbK06z4JbPqnm4ZgAvrgjw';
var doc = DriveApp.getFileById(id); //used ID to get the file saved in your Drive using DriveApp.getFileById() method
var me = Session.getEffectiveUser().getEmail();
MailApp.sendEmail(me, 'test', 'see attachment', {attachments:[doc]}); //attach the file via email
}
Sample Result
You may also check the answers from these similar posts:
Convert Google Doc to PDF using Google Script Editior
Google Apps Script: Send docx email attachment
Send email with an attachment located in Google Drive

Related

How to get XML File parsed from Google Drive without sharing the Link globaly

I'm building a XML parser in Google App Script, witch gets XMLs from Google Drive. At the moment i could only get it to work, when the link of the XML file is shared global.
function XmlChecker(FileID){
var data = UrlFetchApp.fetch("https://drive.google.com/uc?id=" + FileID + "&export=download").getContentText();
var xml = XmlService.parse(data);
var root = xml.getRootElement();
var namespace = XmlService.getNamespace('http://www.editeur.org/onix/2.1/short');
Is there a way to get the same result without sharing the link?
The Google App Scripts is build in a Spreadsheet. Can i give the spreadsheet access via code?
If the file of FileID is in your Google Drive, how about retrieving the data using Drive service as follows?
From:
var data = UrlFetchApp.fetch("https://drive.google.com/uc?id=" + FileID + "&export=download").getContentText();
To:
var data = DriveApp.getFileById(FileID).getBlob().getDataAsString();
By this modification, the data can be retrieved from the file of FileID without publicly sharing.
Reference:
Drive Service

use google apps script copy file to my server

How can I use Google Apps Script to copy a file in my server or move google spreadsheet file on my server.I am really confused. how can I do that?
So if anyone can suggest me to do that it's really helpful to me. Thanks for the help.
Code: it's code to click and download Google Spreadsheet on my desktop.But I need to automatically download on my server (maybe FTP server).
function downloadXLS() {
// Get current spreadsheet's ID, place in download URL
var ssID = SpreadsheetApp.getActive().getId();
var URL = 'https://docs.google.com/spreadsheets/d/'+ssID+'/export?format=xlsx';
// Display a modal dialog box with download link.
var htmlOutput = HtmlService
.createHtmlOutput('Click to download')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(800)
.setHeight(60);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Download XLS');
}

How To set permissions to Google Drive files/folder while adding from Google Site

I am new to Google Sites, I have created a test google site, and add some pages files from google drive to the pages.
My Question is: Is it possible to set permissions from a Google Site while adding a file/folder from Google Drive via any Google Apps Script or Google Drive SDK?
I also did some overview about Google Apps Script for adding UI elements/ html elements, and perform some basic authentication using Google Drive SDK. Given this:
Is possible to perform permission actions.
If we upload a file in a file cabinet template can we set any kind of permissions or remove download link button of that file?
Apps Script has a service called Drive. Drive has a class named: DriveApp. DriveApp can create, save and retrieve files. Once you have a reference to a file, you can use other Classes, Properties and Methods.
setSharing method - Google Documentation
Thanks to Your Answer #Sandy Good from you suggestion/idea I able to create a simple script and after creating this script I am sure I will be able to create a complicated script also.
I have created a script that will create a form in in google site's page. this form contain a dropdown list of my google drive files and an input filed in which i will add an email address.
and in the last a submit button.
after pressing submit button , the script work as it will grant permission on a selected to the email given as viewer of the file if he has link.
create a blank script in the Google Site's Apps Script Section.
Add some UI via UI Service
function doGet() {
// create ui element
var app = UiApp.createApplication();
// create form
var form = app.createFormPanel();
// create area or panel
var flow = app.createFlowPanel();
// create list box for files
var element_file = app.createListBox().setId('file_name').setName('file_name');
// add first elemnt
element_file.addItem('Please Select','');
// get drive files
var files = DriveApp.getFiles();
while (files.hasNext()) {
var file = files.next();
element_file.addItem(file.getName(),file.getId());
}
flow.add(element_file);
var element_text=app.createTextBox().setId('email').setName('email');
flow.add(element_text);
flow.add(app.createSubmitButton("Submit"));
form.add(flow);
app.add(form);
return app;
}
after this I handle the data submitted via post
function doPost(e) {
var app = UiApp.getActiveApplication();
var email = e.parameter.email;
var file_name = e.parameter.file_name;
var public_file = DriveApp.getFileById(file_name);
public_file.addViewer(email);
return app;
}
after this you can check the permissions of the specific file in the google drive.
you may also add a success message.
after this go to publish menu of the file select deploy as web app then select the required things. remember please select execute scripts only as me option because you are listing all your files.
then go to the page in which you want to add script just edit/add new page from editor of the page just insert Apps Script.
apologies , its my first time to work in google apps script , if I miss anything or i did not follow any rule etc.
Thanks,

Write a google app script to store a PDF from URL in a specific folder in google drive

So here is what I want to do:
I have a URL of a PDF document. I want to store it directly into my google drive without downloading it to my hard drive first and then uploading to google drive. Does anyone know how to do this?
Thanks in advance!!!
Using urlFetch and DocsList, this is pretty straightforward and takes only 3 lines of code :
var urlOfThePdf = 'http://www.fostexinternational.com/docs/tech_support/pdfs/280_owners_manual.pdf';// an example of online pdf file
var folderName = 'GAS';// an example of folder name
function saveInDriveFolder(){
var folder = DocsList.getFolder(folderName);// get the folde
var file = UrlFetchApp.fetch(urlOfThePdf); // get the file content as blob
folder.createFile(file);//create the file directly in the folder
}

apps script : xls attach in gmail to google spreadsheet

I try open a xls with app script.
I can get the content type of file with:
var attach = messages[j].getAttachments()[0];
var name = attach.getName();
var type = attach.getContentType();
And I get:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
but, How I can read the information in the cells ?
 EDIT
If I use
DocsList.createFile(attach);
the attach is added in google Docs list.
I solved uploaded the file with drive
https://gist.github.com/4666836