Last active
April 2, 2021 15:09
-
-
Save barrieroberts/894e1f100184ce92063ef56b4eb7283a to your computer and use it in GitHub Desktop.
4-Add & remove files and folders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create 2 folders and file in parent folder, move file & folder1 to folder2 | |
function example41() { | |
const parentFolder = DriveApp.getFolderById('ADD YOUR FOLDER ID HERE'); | |
const folder1 = parentFolder.createFolder("EXAMPLE41-FOLDER1"); | |
const folder2 = parentFolder.createFolder("EXAMPLE41-FOLDER2"); | |
const file = parentFolder.createFile("EXAMPLE41-NEW FILE", | |
"This file has moved from one folder to another.", | |
MimeType.PLAIN_TEXT); | |
file.moveTo(folder2); | |
folder1.moveTo(folder2); | |
} | |
//Make Google Doc, move from My Drive to folder | |
function example42() { | |
var newDoc = DocumentApp.create("EXAMPLE42-New Google Doc"); | |
var newDoc = DriveApp.getFileById(newDoc.getId()); | |
const parentFolder = DriveApp.getFolderById('ADD YOUR FOLDER ID HERE'); | |
newDoc.moveTo(parentFolder); | |
} | |
//Get 2 folders by their IDs and remove one folder from within the other one | |
function example43() { | |
const parentFolder = DriveApp.getFolderById('ADD YOUR FOLDER ID HERE'); | |
const folder = parentFolder.createFolder("EXAMPLE43-FOLDER1"); | |
parentFolder.removeFolder(folder); | |
} | |
//Add shortcut in a specific folder to a file located in another | |
function example44() { | |
const folderCreateSCin = DriveApp.getFolderById('ADD YOUR FOLDER ID HERE'); | |
const fileToLinkTo = DriveApp.getFileById('ADD YOUR DOCUMENT ID HERE'); | |
const folderId = folderCreateSCin.getId(); | |
const targetId = fileToLinkTo.getId(); | |
const shortcutName = "NEW SHORTCUT-To File 'Weekly Presentation'", | |
resource = { | |
shortcutDetails: { targetId: targetId }, | |
title: shortcutName, | |
mimeType: "application/vnd.google-apps.shortcut", | |
}; | |
if (folderId) resource.parents = [{ id: folderId }]; | |
Drive.Files.insert(resource); | |
} | |
//Rev 3 - April 2021 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment