[英]How do I create a file in an existing Google Drive folder
我有這兩個功能。 一個使用文件夾名稱,另一個使用文件夾 ID。 我不明白為什么文件夾 ID 給我“異常:無效參數:parent.mimeType”。 我在兩者中都使用相同的代碼
folder.createFile(文件名,內容)
我嘗試了各種第三個參數,如“text/plain”和其他參數,但沒有結果
// this works fine but will not work for subfolders plus I know the folderID
// so why bother walking the list of folders
function createOrAppendFile1(folderName, fileName, content) {
// get list of folders with matching name
var folderList = DriveApp.getFoldersByName(folderName);
if (folderList.hasNext()) {
// found matching folder
var folder = folderList.next();
// search for files with matching name
var fileList = folder.getFilesByName(fileName);
if (fileList.hasNext()) {
// found matching file - append text
var file = fileList.next();
var combinedContent = content + "\n\n" + file.getBlob().getDataAsString();
file.setContent(combinedContent);
}
else {
// file not found - create new
folder.createFile(fileName, content); // *** this creates the file without an error
}
}
}
// this should be simple and faster since the folderID is known
function createOrAppendFile2(folderID, fileName, content) {
let folder = DriveApp.getFolderById(folderID);
let file, combinedContent;
// search for files with matching name
let fileList = folder.getFilesByName(fileName);
if (fileList.hasNext()) {
// found matching file - append text
file = fileList.next();
combinedContent = content + "\n\n" + file.getBlob().getDataAsString();
file.setContent(combinedContent);
}
else {
// file not found - create new
folder.createFile(fileName, content); // Exception: Invalid argument: parent.mimeType
}
}
Exception: Invalid argument: parent.mimeType
的錯誤消息和createOrAppendFile2(folderID, fileName, content)
createOrAppendFile1(folderName, fileName, content)
發生錯誤的情況,沒有發生錯誤,我猜你當您使用 arguments 運行createOrAppendFile2(folderID, fileName, content)
時的情況, folderID
的文件夾可能不是該文件夾。
folderID
時,我確認了相同的錯誤Exception: Invalid argument: parent.mimeType
。let folder = DriveApp.getFolderById(folderID);
,不會出現錯誤。Exception: Unexpected error while getting the method or property getFolderById on object DriveApp.
發生。 因此,我認為該 ID 是有效 ID。 但是,ID 可能不是文件夾。 為了確認這一點,添加一個腳本來檢查folderID
的值是否是文件夾的ID怎么樣? 當這反映在您的腳本中時,以下修改如何?
function createOrAppendFile2(folderID, fileName, content) {
const mimeType = DriveApp.getFileById(folderID).getMimeType();
if (mimeType != MimeType.FOLDER) {
throw new Error(`Your folder ID "${folderID}" is not folder ID of the folder. MimeType is ${mimeType}.`);
}
let folder = DriveApp.getFolderById(folderID);
let file, combinedContent;
let fileList = folder.getFilesByName(fileName);
if (fileList.hasNext()) {
file = fileList.next();
combinedContent = content + "\n\n" + file.getBlob().getDataAsString();
file.setContent(combinedContent);
} else {
folder.createFile(fileName, content);
}
}
folderID
的值是否為文件夾的ID。 當 ID 用於文件夾時,將運行腳本。 當 ID 不是文件夾時,會發生錯誤。found matching file - append text
,如果你想將append的文本值content
添加到已有的文本文件中, combinedContent = content + "\n\n" + file.getBlob().getDataAsString();
可能需要修改為combinedContent = file.getBlob().getDataAsString() + "\n\n" + content;
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.