The uploaded files are not converted to Google Documents - google-drive-api

I use Google Drive API to upload new files (https://developers.google.com/drive/v2/reference/files/insert), and I use the 'convert' parameter to convert the file in Google Document.
It worked well but today, the uploaded files are not converted.
I tried with a .doc, .docx and .ppt, and I have the same problem.
Does anyone has an idea? Is the API has been changed?

Make sure you are using the correct mime type. See code below.
Drive driveService = new Drive.Builder(...).setApplicationName(...).build();
File fileMetadata = new File();
fileMetadata.setName("file1.doc");
fileMetadata.setMimeType("application/vnd.google-apps.document");
AbstractInputStreamContent inputStream = new InputStreamContent(null,
new FileInputStream(new java.io.File("/file1.doc")));
File file = driveService.files().create(fileMetadata, in).setFields("id").execute();

Related

How do I extract the text from a docx file using Apps Script?

The files are saved in a Drive folder. I need to send the text content of all .docx file as an API payload. I've tried using Blob but to no avail. Is there a way to get this done?
If I understand you correctly, you want to send the text content of a docx file that you have in Drive. If that's correct, then you can do the following:
function docx() {
var docxId ="your-docx-id";
var docx = DriveApp.getFileById(docxId);
var blob = docx.getBlob();
var file = Drive.Files.insert({}, blob, {convert:true});
var id = file["id"];
var doc = DocumentApp.openById(id);
var text = doc.getBody().getText();
return text;
}
This code uses Advanced Drive Service to create a Docs file out of the blob you get from the docx, via Drive.Files.insert. Then, you can easily access this newly created file via DocumentApp and use getText.
Bear in mind that this will create a new file every time you run it. Use Files.delete to avoid that.
I hope this is of any help.

Convert any xls-xlsm files to Google Sheet format and overwrite the oldest existing GS file with that name

I decided to open this topic because there is very little material in this regard and in my opinion many are asking the same question. I'm trying to write an script to convert an .xls or a .xlsm file stored on Google Drive to a Google Sheets file. I'm not interested in converting macros inside the .xlsm file, only converting the base file. This script should be able to overwrite the oldest file with the new one if it has the same name.
The topics in which I found more material are these:
How to automatically import data from uploaded CSV or XLS file into Google Sheets
Converting .xls to google spreadsheet in google apps script
Convert all xls files available in a folder into "Google Doc Spreadsheets"?
these topic links inserted above are partial and in any case they do not allow the conversion of the .xlsm format.
I started from this code (taken from here), but I don't know how to modify it to make it convert both the .xls and .xlsm files and let them overwrite them if they are already in Google Drive
/**
* Sample use of convertExcel2Sheets() for testing
**/
function testConvertExcel2Sheets() {
var xlsId = "0B9**************OFE"; // ID of Excel file to convert
var xlsFile = DriveApp.getFileById(xlsId); // File instance of Excel file
var xlsBlob = xlsFile.getBlob(); // Blob source of Excel file for conversion
var xlsFilename = xlsFile.getName(); // File name to give to converted file; defaults to same as source file
var destFolders = []; // array of IDs of Drive folders to put converted file in; empty array = root folder
var ss = convertExcel2Sheets(xlsBlob, xlsFilename, destFolders);
}
I would like to thank anyone who decided to help me and therefore to give all those who are asking the same question.

Convert html to google docs or docx using drive api v3

I'm trying to convert a html file (or preformatted html String) to Google Docs using drive api v3 and android studio, using these lines:
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("report.html")
.setMimeType("text/html")
.build();
(I extract the code from android-demos-master examples )
If I try put another mimetype like "application/vnd.google-apps.document", my app crash. I want to upload the file and convert to Gdocs editor or Docx. I need convert before or after upload the file. Can someone guide me?
Using the python libraries, I found I had to specify two mimetypes:
Use 'application/vnd.google-apps.document' when creating the metadata for the Drive file. This is the type of file you want created - a Google Document.
Use 'text/html' for the object representing uploaded data, as that is the type of the content. In python, this were objects of type io.MediaUpload (file upload) or io.MediaIoBaseUpload (in-memory content).
I imagine it's something similar in Java.

BOX: How to download file using file url?

I want to use Box SDK to download a file. I have the file link available in local variable
var url = https://mycompany.box.com/s/c565vhytyhx5s85vjg03bgtr0h47d6nh
Currently BoxClient.FilesManager.DownloadStreamAsync(fileid) takes fileid as parameter.
How do I use url to download a file from box using Box SDK?
Use following Code to Download File From Box using box api
BoxFile file=new BoxFile(api, fileID);
FileOutputStream fOut = new FileOutputStream(file path);
URL DownloadUrl=file.getDownloadURL();
BufferedInputStream input=new BufferedInputStream(DownloadUrl.openStream());
and write above file "input" to FileOutputStream with file path where u want

Difference in size of HTML file directly downloaded from Google Drive and via Drive API

I uploaded a word document on Google Drive. I am downloading the file in HTML format in following two ways.
First method: I open the file in Google Docs and then downloaded the file: File -> Download as -> Web page (.html, zipped). I unzip the tar and then I get the HTML file. Its size is 62 kB.
Second method: I used Drive API v3 to create (or upload) the word document to Google Drive. Then I exported the file to HTML format. Its size is 173 kB.
My question is why there is difference of almost three times in size of HTML file? What should be done to get the same file size (62 kB) when downloading (or exporting) using Drive API?
This is the Drive API code I am using to create and export the file.
Drive service = getDriveService();
File fileMetadata = new File();
fileMetadata.setName("Test Document");
fileMetadata.setMimeType("application/vnd.google-apps.document");
FileContent fileContent = new FileContent("application/vnd.openxmlformats-officedocument.wordprocessingml.document", new java.io.File("/home/test/test.doc"));
File createResponse = service.files().create(fileMetadata, fileContent).execute();
java.io.File parentDir = new java.io.File("/home/test/");
if (!parentDir.exists()) {
throw new IOException("Parent directory does not exists.");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, "Test Document"));
service.files().export(createResponse.getId(), "text/html").executeAndDownloadTo(out);
Kindly help me with this issue.
Thanks.