自动移动具有特定名称的文件到Google Drive上的特定文件夹



在我把兔子整只下去之前。我可以自动移动特定的文件到特定文件夹在谷歌驱动器上使用谷歌脚本?

这是设置。我有一堂课,命名为"CAD 142","CAD 242"。和&;加币203…&;因此,录制的会话就这样命名。会话也被逐渐命名为"CAD 142 #01", "CAD 142 #02"....每隔几个小时,我想运行一个自动化的google aps脚本来移动&;CAD 142…&;ClassesCAD 142Fall 2021Recordings"CAD 242…"录音"ClassesCAD 242Fall 2021Recordings"…

只是问这是否可能与谷歌应用程序脚本。这是我知道我可以在windows中用visual basic脚本编写的东西,但需要能够在我的谷歌文件上完成,而不需要我的计算机一直开着。

这个已经存在了吗?这将是这样一个节省时间和减少错误,让我工作的事情,如教学,而不是等待文件处理或忘记他们在等待。

我对google apps脚本还是很陌生。

是。这是可以做到的。你可以设置在Google App Scripts上运行的程序,每隔五分钟触发一次。无论您是否登录,这些都将运行。

您需要设置一个被授权查看/修改您的google驱动器的程序。你可以在这里看到Google App Scripts中所有的方法/对象。

这是我首先开始的一个程序,它捕获了许多Google Drive Files的属性并将它们转储到电子表格中。

function grabFiles() {

var someFiles = DriveApp.searchFiles('modifiedDate > "2020-05-28"');

// var someFiles = DriveApp.searchFiles(
//   'before:2020-01-01 after:2019-01-01 type:pdf');)
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var existingValues = ss.getRange(1, 1, ss.getLastRow(),1).getValues();

existingValues.forEach(function(rows){
rows.forEach(function(aCell){
currentList.push(aCell)
})
})
while(someFiles.hasNext()){
var aFile = someFiles.next();

if(!currentList.includes(aFile.getName())){
aRow = [aFile.getId(),
'=HYPERLINK("' + aFile.getUrl() + '","' + aFile.getName() + '")',
//aFile.getUrl(),
aFile.getDateCreated(),
aFile.getSize()/1000,
aFile.getLastUpdated(),
aFile.getDescription(),
'=HYPERLINK("' + getFolderURL(aFile.getParents()) + '","' + getFolderNames(aFile.getParents()) + '")',
aFile.getOwner().getEmail(),
listEmails(aFile.getEditors()),
listEmails(aFile.getViewers()),
aFile.getMimeType().replace('application/',''),
aFile.getSharingAccess(),
aFile.getSharingPermission()
];
ss.appendRow(aRow)
}
}
}

这是可能的。

对于这个特殊的例子,我用以下假设来做:

  • CAD XXX为类/组
  • 目标文件夹路径为Classes/<class name>/<quarter of the year> <year>/Recordings
  • 季节只是一年中四季的名称
const SEASONS = [
'Winter',
'Spring',
'Summer',
'Autumn',
]
function moveRecordings() {
// Get the recordings folder
const recFolder = DriveApp.getRootFolder().getFoldersByName('Meet Recordings').next()
// Get the files to move
const files = recFolder.getFiles()
// Alternatively you can add further requirements by using `searchFiles` instead:
// const files = recFolder.searchFiles(`createdDate > '2021-07-01' and name constains 'CAD'`)
while (files.hasNext()) {
const file = files.next()
const name = file.getName()
const created = file.getDateCreated()
// Extract the class name from the recording name using a regular expression
const re = /^CAD d{3}/.exec(name)
if (!re) continue
const className = re[0]
// Get the season from the creation date
const season = SEASONS[Math.floor(created.getMonth() / 3)]
// Move the file by using a helper function
const destination = folderFromPath(`Classes/${className}/${season} ${created.getFullYear()}/Recordings`)
file.moveTo(destination)
}
}

/**
* Gets a folder by the given path. If it doesn't exist, it creates the necessary folders.
* 
* It doesn't support relative paths.
* 
* @param {String} path Path representing where the folder is.
* @param {DriveApp.Folder} parent Root folder to look from.
* @returns {DriveApp.Folder}
*/
function folderFromPath(path, parent = null) {
let folder = parent || DriveApp.getRootFolder()
for (let part of path.split('/')) {
folder = getOrCreateFolder(part, folder)
}
return folder
}

/**
* Gets a folder by name or creates one if it doesn't exist.
* 
* If multiple exist, the first one given by Apps Scripts will be returned.
* 
* @param {String} name Name of the folder.
* @param {DriveApp.Folder} parent Parent folder to get the folder from.
* @returns {DriveApp.Folder}
*/
function getOrCreateFolder(name, parent = null) {
if (!parent)
parent = DriveApp.getRootFolder()
const it = parent.getFoldersByName(name)
return it.hasNext() ? it.next() : parent.createFolder(name)
}

此代码只是迭代所有文件并将它们移动到适当的文件夹中。您只需要提取一些信息,然后将它们移到正确的文件夹中。这两个实用函数是不言自明的,它们是对我已经拥有的代码的轻微修改。

有一个部分的替代代码。它可以用来避免移动旧的录音,或录音不是从一个类(不与CAD开始)。如果你觉得合适,可以随意修改。

即使有一些不正确的假设,你应该能够修改这段代码,使它成为你自己的。

您需要做的唯一一件事是每隔几个小时创建一个触发器来调用函数moveRecordings

引用

  • 录制视频会议(Google meeting Help)
  • 类DriveApp (Apps Script reference)
  • class Folder (Apps Script reference)
  • 类文件(Apps Script引用)

最新更新