สร้างไฟล์ png จาก Google Drawing โดยใช้ App Script
Aug 19 2020
ฉันมีโฟลเดอร์ที่เต็มไปด้วย Google วาดเขียน ฉันมีชื่อไฟล์ของ Google วาดเขียนใน Google สเปรดชีต ฉันสามารถแยกชื่อไฟล์จากสเปรดชีตของ Google วนซ้ำผ่านชื่อไฟล์ค้นหาภาพวาดของ Google ทั้งหมดและ ... นี่คือจุดที่ฉันติดขัด ฉันต้องการแปลงภาพวาดเป็นไฟล์ PNG และเก็บไฟล์ PNG ไว้ในโฟลเดอร์ไดรฟ์แยกต่างหาก
นี่คือสคริปต์ที่ฉันมีจนถึงตอนนี้ ...
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());
}
}
}
เมื่อฉันเรียกใช้สิ่งนี้ภาพวาดของ Google ทั้งหมดจะถูกแปลงเป็นไฟล์ PDF และเก็บไว้ในโฟลเดอร์ไดรฟ์ของฉัน
คำตอบ
1 ziganotschka Aug 19 2020 at 15:02
คุณต้องระบุmimeType ที่คุณต้องการแปลงรูปวาด
อย่างไรก็ตามการแปลงจากdrawings
ที่จะpng
เป็นไปไม่ได้โดยตรงคุณจำเป็นต้องดำเนินการตามขั้นตอนต่อไปนี้:
- สร้างลิงค์การส่งออกสำหรับการส่งออกไฟล์เป็น
image/png
- ดึงลิงค์นี้ด้วยUrlFetchApp
- สร้างไฟล์จากหยดของผลลัพธ์
ตัวอย่าง
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);