希望有人可以帮助我。我想创建一个脚本,每24小时将一个表格存档到目前为止我有一个脚本,通过电子邮件发送它。
我想制作一个脚本,如果该文件夹不存在,则在“Year”中创建一个新文件夹,在“month”中创建一个子文件夹,并在时间戳内放置一份带有传输日期的工作表副本。我希望找到一个解决方案,使其成为一个自动化脚本。
function makeCopy() {
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate;
var destination = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())
file.makeCopy(name, destination);
}
干杯。
编辑
我现在已经在脚本上取得了一些进展来制作文件夹和子文件夹,但是这样有一种更简单的方法吗?
好吧,我设法得到创建的子文件夹是否有更简单的方法来做到这一点?
function makeFolder() {
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
var folderYearDate = Utilities.formatDate(new Date(), "GMT", "yyyy");
var folderMonthDate = Utilities.formatDate(new Date(), "GMT", "MMMM");
var name = "Archive - " + formattedDate; // file name
var parent = DriveApp.getFolderById("xxx").getFoldersByName("Archive")
var yearExist = parent.next();
var yearFolders = yearExist.getFoldersByName(folderYearDate);
if(yearFolders.hasNext()){
var yearFolder = yearFolders.next().getId();
Logger.log('Yes, There is a year folder.')
//return;
} else {
var yearFolder = yearExist.createFolder(folderYearDate).getId();
Logger.log('There was no year folder. But created')
//return;
}
var child =
DriveApp.getFolderById("1qZh_O3kbDfDEL2QJoU7U1tUudmjSgWVS").getFoldersByName(folderYearDate)
var monthExist = child.next();
var monthFolders = monthExist.getFoldersByName(folderMonthDate);
if(monthFolders.hasNext()){
var yearFolderExist = yearFolders.next().getName();
Logger.log('Yes, There is a month folder.')
return;
} else {
monthExist.createFolder(folderMonthDate);
Logger.log('There was no month folder. But created')
return;
}
}
新编辑
我已经编译了新的脚本,并且如果有人知道更清洁的方式再做一点工作,那么我也可以通过一点点工作来制作存档数据的副本。非常感谢。
function makeFolder() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("ARCHIVE");
var CurrentDate = new Date();
var oldDate = new Date();
oldDate.setDate(CurrentDate.getDate()-1);
var formattedDate = Utilities.formatDate(oldDate, "GMT", "yyyy-MM-dd");
var folderYearDate = Utilities.formatDate(oldDate, "GMT", "yyyy");
var folderMonthDate = Utilities.formatDate(oldDate, "GMT", "MMMM");
var name = "Archive - " + formattedDate; // file name
var parent = DriveApp.getFolderById("xxx").getFoldersByName("Archive")
var yearExist = parent.next();
var yearFolders = yearExist.getFoldersByName(folderYearDate);
if(yearFolders.hasNext()){
var yearFolder = yearFolders.next()
Logger.log('Yes, There is a year folder.')
//return;
} else {
var yearFolder = yearExist.createFolder(folderYearDate)
Logger.log('There was no year folder. But created')
//return;
}
var monthFolders = yearFolder.getFoldersByName(folderMonthDate);
if(monthFolders.hasNext()){
var monthFolder = monthFolders.next()
Logger.log('Yes, There is a month folder.')
} else {
var monthFolder = yearFolder.createFolder(folderMonthDate);
Logger.log('There was no month folder. But created')
}
var folderId = monthFolder.getId();
var resource = {
title: name,
mimeType: MimeType.GOOGLE_SHEETS,
parents: [{ id: folderId }]
}
var fileJson = Drive.Files.insert(resource)
var fileId = fileJson.id
Logger.log(fileId)
var destination = SpreadsheetApp.openById(fileId);
sheet.copyTo(destination);
var removeSheet = destination.getSheetByName("Sheet1")
var reName = destination.getSheetByName("Copy of ARCHIVE");
reName.setName("ARCHIVE");
destination.deleteSheet(removeSheet);
}
您的解决方案非常简单,只需进行一些小的更改。主要是删除冗余代码。 1)您可以获取文件夹对象本身,而不是获取年份文件夹的ID。
if(yearFolders.hasNext()){
var yearFolder = yearFolders.next()
} else {
var yearFolder = yearExist.createFolder(folderYearDate)
}
2)直接利用此文件夹对象查找具有月份名称的文件夹。
var monthFolders = yearFolder.getFoldersByName(folderMonthDate);
3)现在与以前类似,您可以创建或获取文件夹对象
if(monthFolders.hasNext()){
var monthFolder = monthFolders.next()
Logger.log('Yes, There is a month folder.')
} else {
var monthFolder = yearFolder.createFolder(folderMonthDate);
Logger.log('There was no month folder. But created')
}
monthFolder.createFile(name, "SO help File")
monthFolder.addFile(file)
注意:您可以使用createFile
从头开始创建新文件,或者如果您已经创建了文件,则可以使用addFile
函数将其添加到文件夹中。
你的最终代码:
function makeFolder() {
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
var folderYearDate = Utilities.formatDate(new Date(), "GMT", "yyyy");
var folderMonthDate = Utilities.formatDate(new Date(), "GMT", "MMMM");
var name = "Archive - " + formattedDate; // file name
var parent = DriveApp.getFolderById("xxx").getFoldersByName("Archive")
var yearExist = parent.next();
var yearFolders = yearExist.getFoldersByName(folderYearDate);
if(yearFolders.hasNext()){
var yearFolder = yearFolders.next()
Logger.log('Yes, There is a year folder.')
//return;
} else {
var yearFolder = yearExist.createFolder(folderYearDate)
Logger.log('There was no year folder. But created')
//return;
}
var monthFolders = yearFolder.getFoldersByName(folderMonthDate);
if(monthFolders.hasNext()){
var monthFolder = monthFolders.next()
Logger.log('Yes, There is a month folder.')
} else {
var monthFolder = yearFolder.createFolder(folderMonthDate);
Logger.log('There was no month folder. But created')
}
monthFolder.createFile(name, "SO help File")
}