簡體   English   中英

如何在現有的 Google 雲端硬盤文件夾中創建文件

[英]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的文件夾可能不是該文件夾。
    • 當我對此進行測試時,當我使用文件 ID(例如,它是電子表格。)這是有效文件夾的文件夾 ID 作為folderID時,我確認了相同的錯誤Exception: Invalid argument: parent.mimeType
    • 不幸的是,在當前階段,當使用不是文件夾的 ID 時let folder = DriveApp.getFolderById(folderID); ,不會出現錯誤。
    • 例如,當使用無效的文件夾 ID 時,會出現類似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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM