Add convert xls to Google sheets feature to function of download - google-apps-script

I have working code for download any file in google docs by link. Most of files in xlx or xlsx format and need to be convert in google sheets type. Ive been tried to use some methods, that I find in Internet, but they dont work for me. In code below, I comment with code wich I test. And I guess, that google had changed some documentation
function downloadFile(fileURL,folder) {
var fileName = "";
var fileSize = 0;
var fileId = "";
var response = UrlFetchApp.fetch(fileURL, {muteHttpExceptions: true});
var rc = response.getResponseCode();
if (rc == 200) {
var fileBlob = response.getBlob() //.getAs(MimeType.GOOGLE_SHEETS) - get error
var folder = DriveApp.getFolderById(folder);
if (folder != null) {
var file = folder.createFile(fileBlob);//.getAs(MimeType.GOOGLE_SHEETS) - get error
fileName = file.getName();
fileSize = file.getSize();
fileId = file.getId();
}
}
//file.setMimeType("application/vnd.google-apps.spreadsheet") - not work
//makeCopy('ssssss', folder, {convert: true}) - get error
var fileInfo = [ rc, fileName, fileSize, fileId ];
return fileInfo;
}
I call function like this:
downloadFile("http://www.beltools.ru/prais_rar/price%20TD%20RI.xls","0B_E2P3ZhQySBY3BBMzdlazBLcTA")

In order to convert Excel to Spreadsheet, DriveApp cannot do it. So Drive API has to be used. You can use Drive API v2 from Advanced Google services. "Drive API v2" can be used at Google Apps Script by enabling Drive API of Advanced Google services and of Google API Console.
How to use it is as follows.
In the script editor, select Resources > Advanced Google services
In the dialog that appears, click the on/off switch for Drive API v2.
At the bottom of the dialog, click the link for the Google API Console.
In the console, click into the filter box and type part of the name of the API "Drive API", then click the name once you see it.
On the next screen, click Enable API.
Close the Developers Console and return to the script editor. Click OK in the dialog. The advanced service you enabled is now available in autocomplete.
The detail information is https://developers.google.com/apps-script/guides/services/advanced.
At the sample script, at first, a file is downloaded by fileURL, which is Excel file, as blob. The blob data is uploaded to Google Drive using Drive API. In this case, access token is not required. For downloadFile(), the Input data and output data are same to your downloadFile(). File name is retrieved from fileURL.
Script :
function downloadFile(fileURL, folder) {
var filename = fileURL.match(".+/(.+?)([\?#;].*)?$")[1];
var response = UrlFetchApp.fetch(fileURL);
var rc = response.getResponseCode();
var blob = response.getBlob();
var resource = {
"mimeType": "application/vnd.google-apps.spreadsheet",
"parents": [{id: folder}],
"title": filename
};
var res = Drive.Files.insert(resource, blob);
var fileInfo = [rc, res.title, blob.getBytes().length, res.id];
return fileInfo;
}
Result :
[
200,
sample.xlsx,
10000.0,
## file id ##
]
If I misunderstand your question, I'm sorry.

Related

Prevent Google Drive Service API from extracting text when converting images

I've got a script that processes files uploaded to Google Drive and if they are not pdf files it converts them into Google Docs, then converts the Google Doc file to pdf. This all works as required.
The problem I have is if the file to be converted to Google Docs is an image file, the "new" Google Docs file not only contains the image but also text extracted via OCR (I assume). I've tried setting that option to false to no avail.
Please help, all I want is to convert image files to pdf without extracting any text from the images.
function afterUpload(form_upload_items, suid, lname,scans_folder){
var pdf_file_ids, pdf_files, processed_folder, converted_file;
form_upload_items.forEach (function (form_upload_item_id) {
var uploaded_file = DriveApp.getFileById(form_upload_item_id);
const uft = uploaded_file.getMimeType();
Logger.log("item has name of '%s' and is of type '%s'", uploaded_file.
getName(), uft);
processed_folder = scans_folder.getFoldersByName("Processed").next();
var file_description = uploaded_file.getDescription();
if (file_description==null){
file_description=" "
}
if (file_description.includes("Scanned")){
Logger.log("Already scanned '%s'", uploaded_file.getName());
}
else{
uploaded_file.setDescription("Scanned");
file_description = uploaded_file.getDescription();
}
if (file_description.includes("Processed")) {
Logger.log("Already processed %s", uploaded_file.getName());
}
else{
if (uft !=MimeType.PDF && !file_description.includes("Converted")){ // convert file to pdf if required
//Convert to Google docs
const file_blob = uploaded_file.getBlob();
const options ={
ocr: false,
supportsAllDrives: true
}
const res = Drive.Files.insert({ // Drive API of Advanced Google services is used.
"mimeType": "application/vnd.google-apps.document",
"title": uploaded_file.getName() + "_gdocs_conv"
}, file_blob,options);
uploaded_file.moveTo(processed_folder);
uploaded_file.setDescription(file_description+", Converted");
converted_file = DriveApp.getFileById(res.id);
converted_file.moveTo(processed_folder);
const blobPDF = converted_file.getAs(MimeType.PDF);
//finally convert google docs to PDF
uploaded_file = scans_folder.createFile(blobPDF);
Logger.log("Converted %s to pdf",uploaded_file.getName());
}
}
});
const query = "title contains '" +suid+"'"
const scanned_files = scans_folder.searchFiles(query);
pdf_file_ids = [];
pdf_files = [];
while (scanned_files.hasNext()){
const scanned_file = scanned_files.next();
Logger.log("%s %s",scanned_file.getName(), scanned_file.getDescription());
pdf_file_ids.push(scanned_file.getId());
pdf_files.push(scanned_file);
}
var function_result = {};
var documents_folder = scans_folder.getParents().next();
function_result["file_name_prefix"] = suid+" "+lname;
function_result["pdf_files"] = pdf_file_ids;
function_result["documents_folder_id"] = documents_folder.getId();
}
The problem is that you are converting image files into a Google Document, instead of creating a file type that could be converted into a PDF by Google Drive. To do this, insert the image to this newly created file then convert the resulting document into a PDF. Depending on the result that you requires you might create an HTML file, Google Document or a Google Slides (Google Drawings might be the best but there isn't a Google Apps Script service for that).
The "easier" solution is to create an HTML file. Here is a sample adapted from Tanaike's answer to Show images inside a pdf created with Gloogle Apps Script Blob
function myFunction() {
// image
var url = "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a";
var blob = UrlFetchApp.fetch(url).getBlob();
var b64 = blob.getContentType() + ';base64,'+ Utilities.base64Encode(blob.getBytes());
var html =`<img src="data:${ b64 }">`;
var h = HtmlService.createHtmlOutput(html);
// Create from HTML to PDF file.
DriveApp.createFile(h.getAs("application/pdf").setName("text.pdf"));
}
Related
getBlob() causing "Invalid image data." Error. Google Apps Script

How to convert PDF to image using Google Apps Script? [duplicate]

is there a way to create an image (e.g. a png) from a google document?
I really mean an image, not just a pdf. GetAS only creates pdf, but returns an error if contentType is set to image/png or other equivalent formats.
My (actually trivial) code is
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE);
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
var target = source.makeCopy(source,targetFolder);
var newFile = DriveApp.createFile(target.getAs('image/png'));
}
When I run this code, I get the following error (my translation):
The conversion from application/vnd.google-apps.document to image/png is not supported.
Ty
How about this answer?
Reason of error:
makeCopy() returns File object. getAs() cannot be used for this. By this, the error occurs.
Workaround:
Unfortunately, in the current stage, Google Document cannot be directly exported as PNG images. So it is required to think of workarounds. Google Document can be converted to PDF. This answer uses this. As a workaround, I would like to propose to use an external API which is ConvertAPI. I thought that using the external API, the script becomes simple. This a method (PDF to PNG API) of API can be converted from PDF data to PNG data.
When you try this, for example, you can also test this using "Free Package". When you try using "Free Package", please Sign Up at "Free Package" and retrieve your Secret key.
Sample script:
Before you run this script, please retrieve your Secret key and set it.
var secretkey = "###"; // Please set your secret key.
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var url = "https://v2.convertapi.com/convert/pdf/to/png?Secret=" + secretkey;
var options = {
method: "post",
payload: {File: DriveApp.getFileById(SOURCE_TEMPLATE).getBlob()},
}
var res = UrlFetchApp.fetch(url, options);
res = JSON.parse(res.getContentText());
res.Files.forEach(function(e) {
var blob = Utilities.newBlob(Utilities.base64Decode(e.FileData), "image/png", e.FileName);
DriveApp.getFolderById(TARGET_FOLDER).createFile(blob);
});
References:
makeCopy()
getAs()
ConvertAPI
PDF to PNG API of ConvertAPI
Updated on January 11, 2023:
In the current stage, Google Apps Script can use V8 runtime. By this, there are some Javascript libraries that can be used with Google Apps Script. Ref1, Ref2 In this question, in the current stage, by using pdf-lib, all pages in a PDF file can be converted to PNG images using Google Apps Script. The sample script is as follows.
Sample script:
This method uses Drive API. Please enable Drive API at Advanced Google services.
Please set SOURCE_TEMPLATE and TARGET_FOLDER, and run main().
/**
* This is a method for converting all pages in a PDF file to PNG images.
* PNG images are returned as BlobSource[].
* IMPORTANT: This method uses Drive API. Please enable Drive API at Advanced Google services.
*
* #param {Blob} blob Blob of PDF file.
* #return {BlobSource[]} PNG blobs.
*/
async function convertPDFToPNG_(blob) {
// Convert PDF to PNG images.
const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";
eval(UrlFetchApp.fetch(cdnjs).getContentText()); // Load pdf-lib
const setTimeout = function (f, t) { // Overwrite setTimeout with Google Apps Script.
Utilities.sleep(t);
return f();
}
const data = new Uint8Array(blob.getBytes());
const pdfData = await PDFLib.PDFDocument.load(data);
const pageLength = pdfData.getPageCount();
console.log(`Total pages: ${pageLength}`);
const obj = { imageBlobs: [], fileIds: [] };
for (let i = 0; i < pageLength; i++) {
console.log(`Processing page: ${i + 1}`);
const pdfDoc = await PDFLib.PDFDocument.create();
const [page] = await pdfDoc.copyPages(pdfData, [i]);
pdfDoc.addPage(page);
const bytes = await pdfDoc.save();
const blob = Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, `sample${i + 1}.pdf`);
const id = DriveApp.createFile(blob).getId();
Utilities.sleep(3000); // This is used for preparing the thumbnail of the created file.
const link = Drive.Files.get(id, { fields: "thumbnailLink" }).thumbnailLink;
if (!link) {
throw new Error("In this case, please increase the value of 3000 in Utilities.sleep(3000), and test it again.");
}
const imageBlob = UrlFetchApp.fetch(link.replace(/\=s\d*/, "=s1000")).getBlob().setName(`page${i + 1}.png`);
obj.imageBlobs.push(imageBlob);
obj.fileIds.push(id);
}
obj.fileIds.forEach(id => DriveApp.getFileById(id).setTrashed(true));
return obj.imageBlobs;
}
// Please run this function.
async function myFunction() {
const SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
const TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
// Use a method for converting all pages in a PDF file to PNG images.
const blob = DriveApp.getFileById(SOURCE_TEMPLATE).getBlob();
const imageBlobs = await convertPDFToPNG_(blob);
// As a sample, create PNG images as PNG files.
const folder = DriveApp.getFolderById(TARGET_FOLDER);
imageBlobs.forEach(b => folder.createFile(b));
}
When this script is run, all pages of the inputted PDF file are converted to PNG images, and those images are created in the destination folder.
Note:
I think that the above script works. But, in this case, when you directly copy and paste the Javascript retrieved from https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js to your Google Apps Script project, the process cost for loading it can be reduced.
References:
pdf-lib
copyPages of pdf-lib
addPage of pdf-lib
I know this is an older question, but I thought I'd answer, since I believe I've found a solution that doesn't involve paying for a third-party subscription.
This can be accomplished by accessing the thumbnail of the Doc and creating a new PNG file from that thumbnail. Try this:
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE).getThumbnail().getAs('image/png');
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
TARGET_FOLDER.createFile(source);
}
However, I've found that getting the thumbnail of the Doc is not as high quality as getting the thumbnail of a PDF created from the Doc. You can try the code below to compare which version of the new PNG you prefer.
To do this, you will also need to enable Advanced Services on your project, specifically the Drive API service. To do this, follow these instructions to add a new Service to your Google Apps Script project:
Open the Apps Script project.
At the left, click Editor < >.
At the left, next to Services, click Add a service +.
Select Drive API and click Add.
Once you do that, you'll be able to use the Drive command in your script, which is different than DriveApp. Note also the update to source.makeCopy() to only include the TARGET_FOLDER:
function convertFile() {
var SOURCE_TEMPLATE = "1HvqYidpUpihzo_HDAQ3zE5ScMVsHG9NNlwPkN80GHK0";
var TARGET_FOLDER = "1Eue-3tJpE8sBML0qo6Z25G0D_uuXZjHZ";
var source = DriveApp.getFileById(SOURCE_TEMPLATE);
var targetFolder = DriveApp.getFolderById(TARGET_FOLDER);
var target = source.makeCopy(targetFolder);
var pdfBlob = target.getAs(MimeType.PDF);
var newPDF = TARGET_FOLDER.createFile(pdfBlob).setName('Some Name.pdf');
var newId = newPDF.getId();
Drive.Files.update({
title: newPDF.getName(), mimeType: MimeType.PDF
}, newId, pdfBlob);
var newFile = DriveApp.getFileById(newId).getThumbnail().getAs('image/png');
TARGET_FOLDER.createFile(newFile);
target.setTrashed(true);
newPDF.setTrashed(true);
}
This code will create a copy of your Google Doc file, convert it to a PDF, then grab the thumbnail of the PDF as a PNG, and then delete the copy of the Doc file and the PDF that were created.
The Drive.Files.update() function is the critical part of this code, as it finalizes the creation of the PDF file in your Drive. Trying to run the code without that portion will just return the new PDF file as null since the new PDF hasn't completely finished being created at that point.
Hope this helps!

Google Apps Script Wants Unrestricted Scope

Simple function here in Google Apps Script:
function driveSearch() {
// Log the name of every file in the user's Drive whose visibility is anyonewithLink or anyonecanfind
var files = DriveApp.searchFiles(
'visibility = "anyoneWithLink" or visibility = "anyoneCanFind"');
while (files.hasNext()) {
var file = files.next();
var owner = file.getOwner().getName();
var sa = file.getSharingAccess();
Logger.log(file.getName());
Logger.log('Owner:'+owner);
Logger.log("SharingAccess:"+sa);
}
}
It want to find shared files in my gsuite drive.
However, it says I don't have permissions to run DriveApp
My permissions are set and requested correctly, like so:
{
"timeZone": "America/New_York",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/drive.metadata.readonly"
]
}
The error is
You do not have permission to call DriveApp.searchFiles. Required permissions: https://www.googleapis.com/auth/drive (line 3, file "Code")
Why does searchFiles require delete permissions? I am very worried about inadvertent deletion, I don't want the full scope. Is there something else I'm doing wrong?
Lastly, yes my Gsuite allows Google Apps Script.
How about this workaround? In this workaround, the endpoint of Drive API is directly requested by UrlFetchApp, and the following 2 scopes are used.
https://www.googleapis.com/auth/drive.metadata.readonly
This is used to the access token which is used for using Drive API.
https://www.googleapis.com/auth/script.external_request
This is used for using UrlFetchApp.
Enable Drive API at API console
Before you use this script, please enable Drive API as follows.
On script editor
Resources -> Cloud Platform project
View API console
At Getting started, click "Explore and enable APIs".
At left side, click Library.
At "Search for APIs & services", input "Drive". And click Drive API.
Click Enable button.
If API has already been enabled, please don't turn off.
Sample script:
function myFunction() {
var baseUrl = "https://www.googleapis.com/drive/v3/files";
var q = 'visibility = "anyoneWithLink" or visibility = "anyoneCanFind"';
var fields = "files(id,name,owners,permissions),nextPageToken";
var pageToken = "";
var results = [];
var params = {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}};
do {
var endpoint = baseUrl + "?pageSize=1000&q=" + encodeURIComponent(q) + "&fields=" + encodeURIComponent(fields) + "&pageToken=" + pageToken;
var res = UrlFetchApp.fetch(endpoint, params);
res = JSON.parse(res.getContentText());
Array.prototype.push.apply(results, res.files);
pageToken = res.nextPageToken || "";
} while (pageToken);
results.forEach(function(e) {
Logger.log(e.name);
Logger.log('Owner: ' + e.owners.map(function(f) {return f.displayName}).join(","));
Logger.log("SharingAccess: " + e.permissions.map(function(f) {return f.id}).join(","));
});
}
Note:
In this sample script, the log is the same with your script.
References:
Drive API
list
UrlFetchApp
If this was not the result you want, I apologize.

DriveApp conversion from DocX to PDF fails

I'm trying to convert an existing .DOCX file on my Google Drive.
It works up until the new (PDF) file should be created, then I get this error message:
"Conversion from application/vnd.openxmlformats-officedocument.wordprocessingml.document to application/pdf failed".
The relevant DOCX file should not be corrupted as it can be opened by Google Document and manually convertred to PDF from there.
I have enabled Drive API (both in Drive and in API Console)
Anyone else seen this problem?
Code:
function convertPDF(fileid) {
var docId = fileid;
var f=DriveApp.getFileById(docId);
var n=f.getName();
var docFolder = f.getParents().next().getId();
var docblob = f.getAs('application/pdf');
n=n.replace(".docx",".pdf");
docblob.setName(n);
var bn=docblob.getName();
var t=docblob.getContentType();
Logger.log(bn+"-->"+t); // <-- works
var file = DriveApp.createFile(docblob); // <<- error msg here
var fileId = file.getId();
moveFileId(fileId, docFolder);
return (fileId);
}
At Google, it cannot be directly converted from docx to pdf. But, after it converted docx to Google document, it can convert to pdf. In order to convert docx to Google document, you can use Drive API using Advanced Google services. For this, please enabling Drive API of Advanced Google services as follows.
In the script editor, select Resources > Advanced Google services
In the dialog that appears, click the on/off switch for Drive API v2.
Click OK button.
The detail information of Advanced Google services is here.
I think that enabling Drive API at API console has already been done, because you have already used DriveApp on your script. Google automatically enables Drive API on API console when DriveApp is used in the script.
The modified script using Drive API is as follows.
Modified script :
function convertPDF(docx_id) {
var docx = DriveApp.getFileById(docx_id);
var docFolder = docx.getParents().next().getId();
var n = docx.getName();
var res = Drive.Files.insert({ // Here, Drive API of Advanced Google services is used.
"mimeType": "application/vnd.google-apps.document",
"title": n
}, docx.getBlob());
var f = DriveApp.getFileById(res.id).getBlob();
var docblob = f.getAs('application/pdf');
n=n.replace(".docx",".pdf");
docblob.setName(n);
var bn=docblob.getName();
var t=docblob.getContentType();
Logger.log(bn+"-->"+t);
var file = DriveApp.createFile(docblob);
var fileId = file.getId();
Logger.log(fileId)
moveFileId(fileId, docFolder);
return (fileId);
}
I don't know about moveFileId(). So if an error occurs at this line, please check the function.
Note :
In this case, Google document is created as a temporally file for converting to PDF. The filename is the same to the docx file. So if it is not required for you, please delete it.

Convert PDF to Doc with Google Script Editor

I'm looking to convert PDF to google Doc in Drive using Google Script Editor. However keep getting error message "Converting from application/pdf to application/doc is not supported". Is this conversion possible in script? My attempt as below, would be great to hear any advice.
function convertdPDF2doc() {
var pdffile = DriveApp.getFileById();
var pdfblob = pdffile.getAs('application/doc');
pdfblob.setName(doc.getName() + ".doc");
DriveApp.createFile(docblob);
}
function pdfToDoc() {
var fileBlob = DriveApp.getFileById('0B3m2D6239t6aWHo5TVpyYzhxV1U').getBlob();
var resource = {
title: fileBlob.getName(),
mimeType: fileBlob.getContentType()
};
var options = {
ocr: true
};
var docFile = Drive.Files.insert(resource, fileBlob, options);
Logger.log(docFile.alternateLink);
}
Add Drive API to google apps script project
https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services
You can see you pdf file id on google drive when you download it http://i.imgur.com/3pYvwjx.png