I have come across this guide to change the thumbnail of a .zip file. Though, I am not strong in python, and would prefer a google scripts alternative. Is this possible?
Thankyou in advance
I believe your goal is as follows.
You want to change the thumbnail of a ZIP file using Google Apps Script.
In this case, how about the following sample script?
Sample script:
Before you use this script, please enable Drive API at Advanced Google services. And, please set the file IDs of ZIP file and the image file you want to use as the new thumbnail image.
function myFunction() {
const zipFileId = "###"; // Please set the file ID of zip file.
const imageFileId = "###"; // Please set the file ID of the image file.
const metadata = {
thumbnail: {
image: Utilities.base64EncodeWebSafe(DriveApp.getFileById(imageFileId).getBlob().getBytes()),
mimeType: "image/png",
}
};
Drive.Files.update(metadata, zipFileId); // or const res = Drive.Files.patch(metadata, zipFileId);
}
Note:
About the limitation of the thumbnail, please check this thread.
When you want to use the downloaded image as the thumbnail, please modify image: Utilities.base64EncodeWebSafe(DriveApp.getFileById(imageFileId).getBlob().getBytes()), as follows.
image: Utilities.base64EncodeWebSafe(UrlFetchApp.fetch("URL").getContent()),
In the current stage, Drive API of Advanced Google services is the version 2. By this, the request body is different from Drive API v3. Please be careful this.
Reference:
Files: update
Related
Just a simple question as titled; I have a pdf file which only contains text then I want to load it and extract text.
I believe your goal is as follows.
You want to retrieve the text data from PDF data including only texts using Google Apps Script.
In this case, how about the following flow?
Convert PDF to Google Document using Drive API as a temporal file.
Export text from the created Google Document.
When this is reflected in a script, it becomes as follows.
Sample script:
In this sample, Drive API is used. So, before you test this script, please enable Drive API at Advanced Google services.
function myFunction() {
const fileId = "###"; // Please set the file ID of PDF file on Google Drive.
// Convert PDF to Google Document.
const docId = Drive.Files.copy({title: "temp", mimeType: MimeType.GOOGLE_DOCS}, fileId).id;
// Retrieve text from Google Document.
const text = DocumentApp.openById(docId).getBody().getText();
// If you want to remove the template Google Document, please run this script.
// Drive.Files.remove(docId);
console.log(text); // You can see the retrieved text in the log.
// DriveApp.createFile("sample.txt", text); // If you want to save the text as a file, please use this line.
}
References:
Files: copy
getText()
Google has somewhat recently rolled out the ability to insert audio files from your Drive into Slides with various playback options.
I cannot find any documentation on how to insert a file through Google Scripts but can do so going through the available menu options. I tried using the insertVideo method but got an error
"Exception: The parameters (DriveApp.File) don't match the method signature for SlidesApp.Slide.insertVideo."
Here is a general function I'm trying to get to work (NOOB disclaimer goes here):
function uploadAudioToCurrentSlide(){
var presentation = SlidesApp.getActivePresentation();
var currentSlide = presentation.getSlides()[0];
var audioFile = DriveApp.getFileById('idofaudiofileindrive');
currentSlide.insertVideo(audioFile);
}
Any help is most appreciated!
You want to insert a audio file in Google Drive to Google Slides using Google Apps Script.
Issue and workaround:
I think that the reason of your issue is that the file object is directly used to the method of insertVideo. The argument of insertVideo is the URL and the video object which is not the file object. By this, such error occurs.
In the current stage, when the method of insertVideo is used, the video content is required to be the publicly shared YouTube URL.
And also, it seems that the audio file cannot be directly inserted.
Unfortunately, it seems that these are the current specification. So as a workaround, how about the following flow?
At first, convert the audio file to a video file like MP4. As a test, this can be done at other site. But I'm not sure about the file type of your audio file.
Insert the converted MP4 file on Google Drive using Slides API.
When the Slides API is used, you can insert the video file in Google Drive to the Google Slides. In this sample script, "CreateVideoRequest" of the batchUpdate method of Slides API is used.
Sample script:
Before you run the script, please enable Slides API at Advanced Google services.
function myFunction() {
var fileId = "###"; // Please set the file ID of the converted video file on Google Drive.
var presentation = SlidesApp.getActivePresentation();
var currentSlide = presentation.getSlides()[0];
var resource = {requests: [{createVideo: {source: "DRIVE", id: fileId, elementProperties: {pageObjectId: currentSlide.getObjectId()}}}]};
Slides.Presentations.batchUpdate(resource, presentation.getId());
}
Note:
When you can upload the audio file to YouTube and publicly share it, you can use your script using the URL of the YouTube.
References:
insertVideo(videoUrl)- Advanced Google services
Method: presentations.batchUpdate
CreateVideoRequest
Using prior articles and questions found within stack overflow I was able to find a snippet of App Script that searches Gmail labels for attachments and moves them to a specific folder in Google Drive.
function saveAttachmentInFolder(){
var folder = DriveApp.getFolderById('xxosi2');
var userId = "please.thanks#gmail.com";
var query = "label:thankyoucards-reports";
var res = Gmail.Users.Messages.list(userId, {q: query});//I assumed that this works
res.messages.forEach(function(m){
var attA=GmailApp.getMessageById(m.id).getAttachments();
attA.forEach(function(a){
folder.createFile(a.copyBlob()).setName(a.getName());
});
});
}
I need to modify this code to perform the following additional functions:
If file exists, overwrite and retain version history
I have also played around with the answer found in the following thread to no avail as I believe this is hard coded in some way and too specific to the one file type (xlsx) Copying attachments from Gmail to Google Drive folder and overwriting old files with Apps Script.
I believe your goal is as follows.
You want to check this using the filename between the existing file in the folder and the attachment file.
You want to overwrite the existing file with the attachment file.
In this case, how about the following modification? In this case, Drive API is used. So, please enable Drive API at Advanced Google services.
From:
folder.createFile(a.copyBlob()).setName(a.getName());
To:
var filename = a.getName();
var files = folder.getFilesByName(filename);
if (files.hasNext()) {
Drive.Files.update({}, files.next().getId(), a.copyBlob(), {supportsAllDrives: true});
} else {
folder.createFile(a.copyBlob()).setName(filename);
}
When this modified script is run, the existing file is searched from the folder using the filename of the attachment file. When the file is found, the file is overwritten by the attachment file. When the file is not found, the file is created as a new file.
Note:
In this modified script, the existing file is overwritten. So, please be careful about this. I would like to recommend using a sample file for testing the script.
Reference:
Files: update of Drive API v2
I would like to download images from the web to a drive folder just by writing the image link.
for example I want to download the image is in a drive folder automatically with google apps script.
https://i.imgur.com/QCFXhpg.jpg
It would be like a kind of web scraping in which I get the images of said web in a folder.
function myFunction() {
var destination_id = '1cgzNUb88ZHXSQ7iZ0zYigH1f7KD_7geH' //// Id of the folder in which we are going to store the image
var img = ...;
var destination = DriveApp.getFolderById(destination_id)
destination.createFile(img)
}
In that case, I think that the image can be retrieved using UrlFetchApp. So in the case of your script, how about the following modification?
From:
var img = ...;
To:
var img = UrlFetchApp.fetch("https://i.imgur.com/QCFXhpg.jpg").getBlob();
Reference:
fetch(url)
Google has somewhat recently rolled out the ability to insert audio files from your Drive into Slides with various playback options.
I cannot find any documentation on how to insert a file through Google Scripts but can do so going through the available menu options. I tried using the insertVideo method but got an error
"Exception: The parameters (DriveApp.File) don't match the method signature for SlidesApp.Slide.insertVideo."
Here is a general function I'm trying to get to work (NOOB disclaimer goes here):
function uploadAudioToCurrentSlide(){
var presentation = SlidesApp.getActivePresentation();
var currentSlide = presentation.getSlides()[0];
var audioFile = DriveApp.getFileById('idofaudiofileindrive');
currentSlide.insertVideo(audioFile);
}
Any help is most appreciated!
You want to insert a audio file in Google Drive to Google Slides using Google Apps Script.
Issue and workaround:
I think that the reason of your issue is that the file object is directly used to the method of insertVideo. The argument of insertVideo is the URL and the video object which is not the file object. By this, such error occurs.
In the current stage, when the method of insertVideo is used, the video content is required to be the publicly shared YouTube URL.
And also, it seems that the audio file cannot be directly inserted.
Unfortunately, it seems that these are the current specification. So as a workaround, how about the following flow?
At first, convert the audio file to a video file like MP4. As a test, this can be done at other site. But I'm not sure about the file type of your audio file.
Insert the converted MP4 file on Google Drive using Slides API.
When the Slides API is used, you can insert the video file in Google Drive to the Google Slides. In this sample script, "CreateVideoRequest" of the batchUpdate method of Slides API is used.
Sample script:
Before you run the script, please enable Slides API at Advanced Google services.
function myFunction() {
var fileId = "###"; // Please set the file ID of the converted video file on Google Drive.
var presentation = SlidesApp.getActivePresentation();
var currentSlide = presentation.getSlides()[0];
var resource = {requests: [{createVideo: {source: "DRIVE", id: fileId, elementProperties: {pageObjectId: currentSlide.getObjectId()}}}]};
Slides.Presentations.batchUpdate(resource, presentation.getId());
}
Note:
When you can upload the audio file to YouTube and publicly share it, you can use your script using the URL of the YouTube.
References:
insertVideo(videoUrl)- Advanced Google services
Method: presentations.batchUpdate
CreateVideoRequest