How to get filenames without extension from Google Drive to Google spreadsheet - google-apps-script

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());
}

Related

Google Apps Script: Clone PDFs to new folder

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

How to replace URL within hyperlinks in multiple Google Docs with a Google Apps script

Background: I have about 1500 Google Docs in a Google Services account shared directory. Some of those docs have hyperlinks. I need to replace the URL in hyperlinks with new URLs using a Google Script.
I found this script here. The script below will successfully replace URL's within the body of any Google Doc in my drive, but it will not replace any URL's within hyperlinks.
How can I modify this script to replace the URL within a hyperlink instead of just the body text?
var files = DriveApp.getFiles(); // Note: this gets *every* file in your Google Drive
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
var doc = DocumentApp.openById(file.getId());
doc.replaceText("http://www.googledoclink1.com", "http://www.googledoclinkA.com");
doc.replaceText("http://www.googledoclink2.com", "http://www.googledoclinkB.com");// Note: This will be repeated probably 500 times
}
Logger.log("Done")
}
You need to replace both the text and the hyperlink separately
The hyperlink can be modified with setLinkUrl().
Modify your code in a following way to make it work:
function myFunction() {
var oldLink="http://www.googledoclink1.com";
var newLink="http://www.googledoclinkA.com";
var oldLink2="http://www.googledoclink2.com";
var newLink2="http://www.googledoclinkB.com";
var files = DriveApp.getFiles(); // Note: this gets *every* file in your Google Drive
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
var doc = DocumentApp.openById(file.getId());
var link=doc.getBody().findText(oldLink).getElement().asText();
var link2=doc.getBody().findText(oldLink2).getElement().asText();
link.setLinkUrl(newLink);
doc.replaceText(oldLink, newLink);
link2.setLinkUrl(newLink2);
doc.replaceText(oldLink2, newLink2);
}
Logger.log("Done")
}

Download Folder as Zip Google Drive API

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)

Google App Script to rename a downloaded file

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');
}

Google Apps Script to search Google Drive

Is it possible to use Google Apps Script to search Google Drive for both documents and folders?
Google have killed their own docs/drive search gadget as it appears to rely on iGoogle and Google Enterprise support have admitted this.
Thank you
I think you are looking for SearchFile and SearchFolder of the DriveApp.
The full list of parameters is available in the Google Drive SDK documentation
I've run some tests and seems like it's not possible to do 1 search and get files and folders like it's possible calling the search function from the Google Drive API.
Here a code that list the files and folders with a title that have 2013 in it
function myFunction() {
var searchFor ='title contains "2013"';
var names =[];
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
var file = files.next();
names.push(file.getName());
}
var folders = DriveApp.searchFolders(searchFor);
while (folders.hasNext()) {
var file = folders.next();
names.push(file.getName());
}
for (var i=0;i<names.length;i++){
Logger.log(names[i]);
}
}
Try this piece of code
function searchDrive() {
var folderToSearch = "FolderName";
var folders = DriveApp.getFoldersByName(folderToSearch);
Logger.log(folders);
var fileToSearch = "fileName";
var files = DriveApp.getFilesByName(fileToSearch);
Logger.log(files);
}
This example can be found here.