Reputation: 161
I've got a folder full of Google Drawings. I've got the filenames of the Google Drawings in a Google spreadsheet. I can extract the filenames from the Google spreadsheet, iterate through the filenames, find all the Google drawings and ... this is where I get stuck. I'd like to convert the drawings into PNG files and store the PNG files in a separate drive folder.
This is the script I have so far ...
function load_list_of_images() {
var course_workbook_name = "SPREADSHEET"; // Title of spreadsheet to download files
var course_workbooks = DriveApp.getFilesByName(course_workbook_name); // There may be more than one!
try{
var course_workbook_id = course_workbooks.next();
Logger.log("Spreadsheet ID : " + course_workbook_id);
} catch(error) {
Logger.log("Spreadsheet doesn't exist");
return(null);
}
var course_workbook = SpreadsheetApp.open(course_workbook_id);
var image_list_sheet = course_workbook.getSheetByName("image_list");
// Get list of image names (without extensions)
var list_of_images = [];
var images = image_list_sheet.getRange(1,1,1000).getValues();
for (var row in images) {
for (var col in images[row]) {
if (images[row][col] == "") {
return(list_of_images);
}
list_of_images.push(images[row][col]);
}
}
}
function download_images() {
var list = load_list_of_images();
if (list == null){
return(null);
}
for (var row in list){
var image_name = list[row];
var image_exists = DriveApp.getFilesByName(image_name);
// There may be more than one
if (image_exists.hasNext()) {
var image = image_exists.next()
var gDraw_file = DriveApp.getFileById(image.getId());
DriveApp.createFile(gDraw_file.getBlob());
}
}
}
When I run this, all the Google drawings are converted to PDF files and stored in my drive folder.
Upvotes: 4
Views: 1630
Reputation: 26796
However, converting from drawings
to png
is not possible directly, you need to perform the following steps:
image/png
Sample
var id = image.getId();
var exportUrl = "https://www.googleapis.com/drive/v3/files/" + id + "/export?mimeType=image/png";
var urlFetchOptions = {
headers: {Authorization : "Bearer " + ScriptApp.getOAuthToken()}
};
var blob= UrlFetchApp.fetch(exportUrl, urlFetchOptions).getBlob();
DriveApp.createFile(blob);
Upvotes: 3