Attempting to Attach a CSV, Converting to PDF - csv

I have a code where I am attempting to attach a CSV saved in Drive (automatically replaced) to an email. At the moment, I can get it to add using fileBlob - DriveApp.getFileById(...).getBlob() but, it's attaching as a PDF.
Per Mogsdad's answer on this question there is a way to attach a CSV, but looking through the documentation I can't seem to find anything that will allow using a CSV. In fact, according to the documentation for getAS() it literally says: "For most blobs, 'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of 'image/bmp', 'image/gif', 'image/jpeg', or 'image/png' are also valid."
Does this mean in is impossible to attach a CSV (or, really, anything other than PDF) from Drive to an email using Apps Scripts?

Assuming the CSV file is in your Drive, you can use this snippet to attach it to an email.
function sendCSV() {
var blob = DriveApp.getFileById(FILE_ID).getBlob();
MailApp.sendEmail("email#example.com", "Subject", "CSV", {attachments:[blob]});
}

As per following Google Documentation: -
https://developers.google.com/apps-script/reference/base/blob-source.html
Following is mentioned: -
For most blobs, 'application/pdf' is the only valid option.
Following expected work-around: -
https://gist.github.com/nirajkadam/0b41a01b8e739800c964

Related

Convert application/postscript to JPG

I'm hoping to convert an EPS file to a JPG or a PDF using Google Apps Script, and I'm wondering if it's possible. I started with this code:
var file = DriveApp.getFileById(fileID);
var conversion = file.makeCopy().getAs('image/jpeg');
However, when I do this, I get the following error message:
"Converting from application/postscript to image/jpeg is not supported."
Is it possible to make this conversion?
I believe your goal as follows.
You want to convert EPS file (application/postscript) to the Jpeg format (image/jpeg) using Google Apps Script.
Issue and workaround:
Unfortunately, in the current stage, getAs cannot directly convert from application/postscript to image/jpeg. I think that this is the current specification. So in this case, it is required to use a workaround. In this answer, I would like to propose this workaround. The flow of this workaround is as follows.
Retrieve file metadata using the method of "Files: get" in Drive API.
Retrieve the EPS file (application/postscript) as PNG format, and convert it to Jpeg format.
Create the Jpeg data as a file.
When above flow is used as a script, it becomes as follows.
Sample script:
When you use this, please enable Drive API at Advanced Google services. In this sample script, the EPS file is converted to Jpeg format.
var fileID = "###"; // Please set the file ID of the EPS file.
// 1. Retrieve file metadata using Drive API.
var res = Drive.Files.get(fileID);
// 2. Retrieve the EPS file (`application/postscript`) as PNG format, and convert it to Jpeg format.
var blob = UrlFetchApp
.fetch(res.thumbnailLink.replace("s220", "s1000"))
.getBlob()
.getAs(MimeType.JPEG)
.setName(res.title.split(".")[0] + ".jpg");
// 3. Create the Jpeg data as a file.
DriveApp.createFile(blob);
Note:
In above workaround, the EPS format is converted to the PNG format using Drive API, and the PNG format is converted to the Jpeg format using getAs. But in this workaround, it cannot convert to the PDF format. So if you want to convert the EPS format to the PDF format, I think that the external API like https://www.convertapi.com/eps-to-pdf might be suitable.
References:
getAs(contentType)
Files: get
According to the documentation, you should be able to use getAs('application/pdf')
For most blobs, 'application/pdf' is the only valid option. For images
in BMP, GIF, JPEG, or PNG format, any of 'image/bmp', 'image/gif',
'image/jpeg', or 'image/png' are also valid.

Can Google Apps Script blobs be returned with their content type set to Spreadsheet?

As part of a suite of tools I am developing for the company I work for, I have an add-on in development that when first installed generates all the relevant files and folders for the suite.
Due to the complexity of some of the files I discovered that using the following code was the quickest way to generate the files:
function createTemplate(branchId){
var home=DriveApp.getFolderById(branchId)
var master=DriveApp.getFileById(stringId).getBlob();
home.createFile(master);
}
I am presented with the problem, however, that all the files generate as PDFs. I am aware that this is because the default blob content type is the PDF file type and that getAs(contentType) can be used to specify the desired blob content type; but I have struggled to find any documentation specifying how to call the content type as a Spreadsheet, for example.
Is this possible and I've just missed somewhere in Google's documentation how to specify content type as spreadsheet or is it no possible and can blobs only be returned with content types of PDF or image types?
UPDATE: So I discovered accidentally that the content type for spreadsheets would appear to have be specified by application/vnd.google-apps.spreadsheet However, I now get the error Converting from application/pdf to application/vnd.google-apps.spreadsheet is not supported.
FURTHER UPDATE: Potential solution using different route found using the below script:
function createTemplate(branchId){
var home=DriveApp.getFolderById(branchId);
var master=DriveApp.getFileById("fileId").makeCopy("PP Template", home);
}
Although this had around an 8 second run-time so not the quickest thing in the world. Blob related answer still welcome.
As far as I am aware, blob will not do what you want to at this time. There is a reason that DocumentApp.create, etc. exist. They are the function calls that Google wants you to make for what you want to do. They don't want 13 ways to perform the same function.

Google drive mime types is not correct (Google thinks that is not PDF?);

I get files from google drive(Using Java)
Everything works! BUT, when I see pdf documents mime type
file.getMimeType()
it is something like that: application/vnd.google-apps.document
it is not application/pdf
Question 1: Why is my pdfs at google drive with mime type application/vnd.google-apps.document
Question 2:
I need to filter my document, in order to get only PDF documents:
but this does not works! Googel thinks that my PDF is "vnd.google-apps.document"!
String mime ="application/pdf"; //works when I set vnd.google-apps.document
String query="'"+id+"'" +" in parents and trashed=false and and mimeType="+mime;
FileList files = service.files().list().setQ(query).execute();
I've been reading a number of comments recently that Drive PDFs have become editable. So my guess is that you uploaded your PDFs with convert=true, and so Drive has converted them to docs. To prevent this, make sure that convert=false.

Javascript Writing to PDF or Excel

Hey Related to your answer on Can I write files with HTML5/JS?
How would I modify this for say Excel or PDF. I tried but was unable to get it to work. I get a corrupt file downloaded when I change MIME to application/pdf
I am trying to link it to data stored in localstorage which I have all in a variable.
My current code is:
function setSaveFile(contents, file_name, mime_type) {
var a = document.getElementById('save');
mime_type = mime_type || 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; // text/html, image/png, et c
if (file_name) a.setAttribute('Log.xls', file_name);
a.href = 'data:'+ mime_type +';base64,'+ btoa(contents || 'Description' + ' ' + 'Notes' + ' ' + 'Date\n\n' + pdftimeLog);
}
I want to be able to export it as a CSV with each of those headings: DATE, description, notes in different cells. I want it mobile friendly.Thanks
Excellent Options for CSV Everyone:
http://snapshotmedia.co.uk/blog/jspdf
Is it possible to use any HTML5 fanciness to export local storage to Excel?
http://css-tricks.com/localstorage-examples/
Local Storage manipulation and possible sending
Thanks!
For a file to be a valid pdf or excelfile their contents need to conform to the standard that defines the respective format, just changing mimetypes won't do much good.
For PDF there's a library jspdf that might work for you.
I haven't seen a solution for Excel yet, it will be a whole lot harder as the (xls) file format is quite complicated, for the time being it's probably preferable to generate the file on the server.
As an alternative to CSV, it's also possible to write an Excel file by writing HTML to a file with a .xls extension. Excel opens it up based on the extension. Haven't yet explored how deeply it can paginate, format.

Unable to update mimeType using Google Drive API

I've loaded some files to Google Drive, but mistakenly assigned an incorrect mimeType to some of the files. I now want to update the mimeType to be the correct value.
I have the following Python code, which successfully updates the description associated with the file, but the mimeType always retains its original value.
file = service.files().get(fileId=GoogleFileId).execute()
file['mimeType'] = 'application/msword'
file['description'] = 'test description'
updated_file = service.files().update(fileId=file['id'], body=file, updateViewedDate=False).execute()
Can anyone spot what I'm missing?
Dave
Yes, you'll have to update the file content itself. The mimeType is calculated from the file that got uploaded. Do it by passing a media_body, etc that you used to create the file.
It's a nuisance to do, sorry. But if you are scripting it, it should cause you too much pain for "some files".