please assist.
I have a Google App Script that downloads an Excel file from the internet into my Google Drive. This is working. Thereafter the file needs to be renamed, this I'm struggling with.
Downloading of file working. Struggling with renaming...
function SaveToGoogleDrive()
var folderID = 'foldername'; // put id of the Google Drive folder
var folder = DriveApp.getFolderById(folderID) // get the folder
var file = UrlFetchApp.fetch("thewebsite");
folder.createFile(file);
//file.remame("NewDownloadedFile); // help?
}
Try this:
function SaveToGoogleDrive()
var folder = DriveApp.getFolderById('folderID');
var fileblob = UrlFetchApp.fetch("thewebsite").getBlob();
var file=folder.createFile(fileblob);
file.setName('NewDownLoadFile');
}
Related
I am trying to download a file from a folder in Google Drive to a folder in my local PC using Google Apps Script. However, I couldn't find a proper way to do it. The files are usually .pdf, .csv, or .docx. And I want to save them to a specific folder in my local computer with a trigger, so that the script will work even when my computer is in sleep. Is this even possible?
I am also okay for a solution that works when my PC is open.
For now I am able to reach the files but couldn't find a way to download them to my local.
function downloadFiles() {
var driveApp = DriveApp
var folderIter = driveApp.getFoldersByName('DownloadTrys')
var folder = folderIter.next()
var filesIter = folder.getFiles()
var localFolder = "C:\\Users\\XXXX\\Desktop\\personal\\"
//var file = DriveApp.getFileById("downloadthis.txt");
while(filesIter.hasNext()){
var file = filesIter.next();
var fileName = file.getName();
Logger.log(fileName);
var blob = file.getBlob();
var filePath = localFolder + fileName
Logger.log(filePath)
var data = Utilities.base64Encode(blob.getBytes());
var localfile = Utilities.newBlob(Utilities.base64Encode(data), blob.getContentType(),filePath)
driveApp.createFile(localfile).setName(fileName).setTrashed(true);
};
}```
I know it is possible to convert excel files to Google Sheets using script and drive API, but I'm looking for script to convert the excel sheet and move the converted file to a different folder.
So the required steps are as follows:
Convert excel (.xls/.xlsx) to Google Sheet from FolderA.
Move converted file from FoldarA to FolderB.
Delete original excel file from FolderA
Hopefully step 3 avoids this, but avoid duplicating already converted file.
The excel files are being pasted into a local folder that is being synced to google drive, and the files are no larger than 3mb. The current script is as follows. This is converting the files but placing them in the root folder, and will duplicate the conversion when the script runs again.
function importXLS(){
var files = DriveApp.getFolderById('1hjvNIPgKhp2ZKIC7K2kxvJjfIeEYw4BP').searchFiles('title != "nothing"');
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if (name.indexOf('.xlsx')>-1){
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = { title : name+'_converted',
key : ID
}
file = Drive.Files.insert(newFile, xBlob, {
convert: true
});
}
}
}
You want to create the converted Google Spreadsheet files to "FolderB".
You want to delete the XLSX files in "FolderA" after the files were converted.
You want to achieve above using Google Apps Script.
If my understanding correct, how about this modification? In this modification, I modified your script.
Modification points:
You can directly create the file to the specific folder using the property of parents in the request body.
You can delete the file using Drive.Files.remove(fileId).
Modified script:
Before you use this script, please enable Drive API at Advanced Google services.
function importXLS(){
var folderBId = "###"; // Added // Please set the folder ID of "FolderB".
var files = DriveApp.getFolderById('1hjvNIPgKhp2ZKIC7K2kxvJjfIeEYw4BP').searchFiles('title != "nothing"');
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if (name.indexOf('.xlsx')>-1){
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = {
title : name+'_converted',
parents: [{id: folderBId}] // Added
};
file = Drive.Files.insert(newFile, xBlob, {
convert: true
});
// Drive.Files.remove(ID); // Added // If this line is run, the original XLSX file is removed. So please be careful this.
}
}
}
Note:
If the number of XLSX files is large, the execution time might be over 6 minutes.
About // Drive.Files.remove(ID);, when you run this script, please be careful. Because the original XLSX files are completely deleted when the script is run. So I commented this. At first, please test the script using sample files.
References:
Files: insert
Files: delete
Afternoon all,
I am trying desperately to get a script to work and from what I can see it should work perfectly. I want to get a list of PDFs from the source folder and make copies of said PDFs into a secondary folder.
Here is the script:
var sourceFolder = DriveApp.getFolderById("source");
var targetFolder = DriveApp.getFolderById("target")
var files = sourceFolder.getFilesByType(MimeType.PDF);
while (files.hasNext()) {
var file = files.next();
file.makeCopy(targetFolder);
}
Am I missing something? Do I need permissions to use google scripts on drive? I am very confused.
Any help would be appreciated.
As per Marios mentioned in the comment, your code does work. Just make sure to copy the proper ID of the folder.
Your code should look like the code below when including the folder IDs
function myFunction() {
var sourceFolder = DriveApp.getFolderById("0B55C21aJsSBlfk9FTjRqOG8tb3hjR1N4MTU1YjVPNU4weGVhSldfU3F4OXladVVNMF9Ccms");
var targetFolder = DriveApp.getFolderById("1n9PJ_FFlJvlCRdWxHHJiTn4RF6-a1ypE");
var files = sourceFolder.getFilesByType(MimeType.PDF);
while (files.hasNext()) {
var file = files.next();
file.makeCopy(targetFolder);
}
}
Please see this to see how you get the folder ID.
Note that when you see a ?key=value format, don't include that and anything after that. They are called URL parameters and is not a part of the Folder ID
I currently have a Google App Script in Google Sheet that gives me the URL of a folder, which I can then use to download. Though it is an extra step I would like to remove, and get the URL of the zipped content directly.
Here's my code (google app script):
function downloadSelectedScripts() {
// ...
var scriptFiles = getScriptFiles(scriptFileNames)
var tempFolder = copyFilesToTempFolder(scriptFiles)
Browser.msgBox(tempFolder.getUrl())
}
function copyFilesToTempFolder(files) {
var tempFolder = DriveApp.getFolderById(FOLDERS.TEMP)
var tempSubFolder = tempFolder.createFolder('download_' + Date.now())
for (var i in files) {
var file = files[i]
file.makeCopy(file.getName(), tempSubFolder)
}
return tempSubFolder
}
You want to compress all files in a folder as a zip file.
The folder has no subfolders.
All files are only text files.
The total size of all files is less than 50 MB.
You want to retrieve the URL for downloading the zip file.
If my understanding is correct, how about this sample script? The flow of this script is as follows.
Retrieve folder.
Retrieve blobs of all files in the folder.
Compress blobs and retrieve a blob of zip.
Create a zip blob as a file.
Retrieve URL for downloading.
Sample script:
When you use this script, please set the folder ID of folder that you want to compress.
function myFunction() {
var folderId = "###"; // Please set the folder ID here.
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFiles();
var blobs = [];
while (files.hasNext()) {
blobs.push(files.next().getBlob());
}
var zipBlob = Utilities.zip(blobs, folder.getName() + ".zip");
var fileId = DriveApp.createFile(zipBlob).getId();
var url = "https://drive.google.com/uc?export=download&id=" + fileId;
Logger.log(url);
}
Result:
The direct link of the zip file is returned as follows.
https://drive.google.com/uc?export=download&id=###
Note:
In the current stage, unfortunately, the folders cannot be included in the zip data with Google Apps Script.
In this sample script, the filename of zip file is the folder name. So please modify it for your situation.
If you want to download the zip file without login to Google, please share the file.
Reference:
zip(blobs, name)
Is there any way to fetch files name without their extensions from a Google Drive folder into Google spreadsheet?
Yes, take a look to this.
In the page's example, you can switch DriveApp.getFiles() by myFolder.getFiles(), like this:
var myFolder = DriveApp.getRootFolder();
var files = myFolder.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
}