问题描述
我有此脚本可将电子表格保存在Google云端硬盘Squads
文件夹中.
I have this script to save my spreadsheet in the Google Drive Squads
folder..
已保存文件的名称取决于Gerais
页的单元格H2
中的值,有时会重复该名称,并且在保存时会创建一个新文件,而不是订阅现有文件,想要在此代码中添加以下选项:如果已经存在与该新文件同名的文件,而不是在Google Drive Squads
文件夹中没有两个同名文件,则旧文件将完全消失,并且仅使新文件可用
The name of the saved file is according to the value that is in cell H2
of the Gerais
page, sometimes the name repeats and when saving, a new file is created instead of subscribing to the existing one, I would like to add in this code the option that if a file with the same name as this new one already exists, instead of having two files with the same name in the Google Drive Squads
folder, the old one will disappear completely and only make the new file available
//Create PDF
SpreadsheetApp.flush();
var theurl = 'https://docs.google.com/a/mydomain.org/spreadsheets/d/' + // Best to place the line break after '+'
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' + // SHEET ID
'/export?format=pdf' +
'&size=0' +
'&portrait=true' +
'&fitw=true' +
'&top_margin=0' +
'&bottom_margin=0' +
'&left_margin=0' +
'&right_margin=0' +
'&sheetnames=false&printtitle=false' +
'&pagenum=false' +
'&gridlines=false' +
'&fzr=FALSE' +
'&gid=' +
'XXXXXXXXXXXX'; //SHEET PAGE ID
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(theurl, { headers: { 'Authorization': 'Bearer ' + token } });
var pdfBlob = docurl.getBlob();
// Get filename from sheet "Gerais", cell "H2"
var fileName = spreadsheet.getSheetByName("Gerais").getRange("H2").getValue();
// Create file from blob and name it
// The newFile is placed in the root folder by default
var newFile = DriveApp.createFile(pdfBlob).setName(fileName);
// if folder exists use next
if (DriveApp.getFoldersByName("Squads").hasNext()){
var folder = DriveApp.getFoldersByName("Squads").next();
// if folder does not exist
} else {
var folder = DriveApp.createFolder("Squads");// new folder created in the root folder by default
}
folder.addFile(newFile); // add new file to folder
DriveApp.removeFile(newFile); // remove file from root folder
我也尝试在IF
和ELSE
之间创建交互,以使文件名与文件夹名相同,但尝试失败
I tried to create an interaction between IF
and ELSE
too to make the file name match the same as I did for the folder name but I was unsuccessful in trying
推荐答案
问题
用其名称(可能是唯一的)覆盖Folder
中现有的File
.
Overwriting existing File
in a Folder
by its name (presumably unique).
解决方案
如何检查是否存在将名称写入fileName
变量的文件,如果存在,在添加新创建的文件之前将其删除? 修改将类似于以下内容(您的脚本已通过 API所需的范围):
How about checking if a file with the name written into fileName
variable exists and if so, removing it before adding the newly created one? Modification will look something like this (your script has be authorized with one the scopes required for the API):
//prepare new file and Squads folder;
var existing = folder.getFilesByName(fileName); //returns file iterator;
var hasFile = existing.hasNext(); //check if iterator isn't empty;
if(hasFile) {
var duplicate = existing.next(); //access file;
//delete file;
var durl = 'https://www.googleapis.com/drive/v3/files/'+duplicate.getId();
var dres = UrlFetchApp.fetch(durl,{
method: 'delete',
muteHttpExceptions: true,
headers: {'Authorization': 'Bearer '+token}
});
if(dres.getResponseCode()>=400) {
//handle errors;
}
}
//continue: add file, remove from root, etc;
注释
-
定义查询参数的方式是厄运金字塔的常见情况.此外,每个参数都经过硬编码,这使得导出变得不灵活.为了避免这种情况,请使用对象对其进行配置(因为查询不过是一组键值对):
The way you define query parameters is the common case of a pyramid of doom. Besides, each parameter is hardcoded, which makes the export inflexible. To avoid this, use objects to configure them (as query is nothing more than a set of key-value pairs):
var params = {
format: 'pdf',
size: 0,
portrait: true,
fitw: true,
top_margin: 0,
bottom_margin: 0,
left_margin: 0,
right_margin: 0,
sheetnames: false,
printtitle: false,
pagenum: false,
gridlines: false,
fzr: 'FALSE',
gid: 'XXXXXXXXXXXX'
};
//append params to the url;
var theurl = 'base?'+Object.keys(params).map(function(key){
return key+'='+params[key];
}).join('&');
更新
更正为method
的UrlFetchApp.fetch()
调用应该是参数对象属性,而不是参数.
Corrected UrlFetchApp.fetch()
call as method
should be a parameters object property and not an argument.
参考
- Drive API
delete
参考;
- Drive API
delete
reference;
这篇关于如果Google云端硬盘文件夹中已经存在相同名称的文件,则添加代码以覆盖PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!