我不敢相信没有一种简单的方法可以做到这一点,但我到处寻找可行的解决方案,但没有成功。
我正在 Google Sheet 中运行 Google 脚本,并且我在代码中想要将电子表格另存为新文件名。在我这样做之前,我想检查一下是否已经存在一个(因为我不想覆盖它)。
无论文件夹或文件是否存在,我用来测试的下面的代码都会返回“文件存在”。
与往常一样,非常感谢任何帮助:-)
这是我的测试代码。
function testscriptforfileexistance() {
filecheck = checkfileexists("UnderDevelopment",'20160919 Weekly Roster v1.0.gsheet');
if (filecheck.filefound = true) {
Browser.msgBox("file found") ;
} else {
Browser.msgBox("File not found");
}
}
function checkfileexists(foldername,fn) {
var destFolder = DriveApp.getFoldersByName(foldername);
filefound=false;
var destFileId = DriveApp.getFilesByName(fn);
//if hasnext returns false, presumably it doesn't exist
if (!destFileId.hasNext) {
Logger.log ("Found");
filefound = true;
// and as a second test, if the length is zero the file doesn't exist
if (!destFileId[0]) …Run Code Online (Sandbox Code Playgroud)