希望有人可以帮助我。我想创建一个脚本,每24小时归档一个工作表到目前为止我有一个脚本发送给它。
我想创建一个脚本,如果该文件夹不存在,请按"年"创建一个新文件夹。以及"月"的子文件夹在里面并放置一张带有转移日期的时间表。我希望找到一个解决方案,使其成为一个自动化脚本。
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;
}
}
NEW EDIT
我已经编译了新脚本,并且如果有人知道更清楚的方法,那么我还可以通过一点点解决方法来制作存档数据的副本。非常感谢。
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);
}
答案 0 :(得分:1)
我只是想说找到这段代码对我正在从事的项目非常有帮助。我发现您遇到OP的部分问题。我怀疑该错误与您在makeCopy中使用的变量类型有关。名称必须为字符串,目的地必须为文件夹对象类型。
此外,makeCopy必须使用文件变量类型。如果您要创建工作表,则需要复制整个工作簿,然后使用一些额外的代码删除多余的工作表。
这是我完成的代码,因此您可以了解如何使它正常工作。
override func viewDidLoad() {
super.viewDidLoad()
let localRenderer = RTCMTLVideoView(frame: self.localVideoView?.frame ?? CGRect.zero
let remoteRenderer = RTCMTLVideoView(frame: self.view.frame)
self.view.insertSubview(localRenderer, at: 0)
self.view.insertSubview(remoteRenderer, at: -1)
self.webRTCClient.startCaptureLocalVideo(renderer: localRenderer, position: .front)
self.webRTCClient.renderRemoteVideo(to: remoteRenderer)
答案 1 :(得分:0)
您的解决方案非常简单,只需进行一些小改动。主要是删除冗余代码。
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")
}