如何检索Google Drive文件的所有Get/Is函数结果?

huangapple go评论65阅读模式
英文:

How to Retrieve All Get/Is Function Results of Google Drive File?

问题

总体目标:

我正在循环遍历各种文件,并试图找出一种动态列出/捕获尽可能多的GAS文件对象信息的方法。

如果我采用非动态方法,我将像这样输入要捕获的每个属性:

  var zText = 'File Id:' + aFile.getId();
  zText += "\n" + "getName: " + aFile.getName();
  zText += "\n" + "getDateCreated: " + aFile.getDateCreated();
  zText += "\n" + "getSize: " + aFile.getSize();

等等...

虽然上面的方法最终可以得到我想要的,但我希望避免查找所有属性名称。我知道许多属性可能无法转换为字符串,但我至少希望按名称隔离它们。

我尝试过的方法

我认为我的问题类似于这个问题,该问题使用stringify。但是,这在Google文件对象上不起作用(它只返回一个字符串值{})。我猜想这是因为许多属性在调用函数?

我可以通过使用以下方法将文件的所有属性/函数列为字符串来获取:Object.keys(aFile).forEach((prop)=> Logger.log(prop ));

然而,这并不返回每个函数的结果,而且其中许多函数我不希望执行(例如makeCopy)。

虽然可能有更好的方法,但我假设尝试执行所有以getis开头的属性/函数可能是安全的。我不知道如何做到这一点,也找不到任何与这种用例匹配的内容。我看到了关于使用executeFunctionByName的帖子,但它在App Script环境中不起作用。如果它能够工作,我可能会使用类似这样的东西..

var allKeys =  Object.keys(aFile);
for(var i=0;i<allKeys.length;i++){
 var eachProperty = allKeys[i];
 
 if(eachProperty.search(RegExp("^get|^is"))>-1 ){
  //??? 在aFile上运行函数的字面名称并打印结果?
    Logger.log(eachProperty + ':' + executeFunctionByName(eachProperty,aFile));
 }
}

如果有一个完全更好的方法,我不知道的话,请随时引导我。

英文:

Overall Goal:

I'm looping through a variety of files, and trying to figure out a way to dynamically list/capture as much information from a GAS File Object as possible.

If I were to take a NONdynamic approach, I'd type out each property I want to capture like this:

  var zText = &#39;File Id:&#39; + aFile.getId();
  zText += &quot;\n&quot; + &quot;getName: &quot; + aFile.getName();
  zText += &quot;\n&quot; + &quot;getDateCreated: &quot; + aFile.getDateCreated();
  zText += &quot;\n&quot; + &quot;getSize: &quot; + aFile.getSize();

etc...

While the above approach would eventually get what I want, I'd like to avoid having to lookup all the property names. I'm aware that many properties might not be able to be converted to string, but I'd like to at least have them isolated by name.

What I've Tried

I thought my question was similar to this question that uses stringify. However, that doesn't work on a Google File object (it just returns a string value of {}). I'm guessing this is because a lot of the properties are calling functions?

I can get all of the file's property/functions LISTED as string, by using: Object.keys(aFile).forEach((prop)=&gt; Logger.log(prop ));

However, this doesn't return the RESULT of each function, and many of these are functions I wouldn't want to execute (i.e. makeCopy).

While there may be a better method, I am assuming I'd probably be safe to try to execute all the properties/functions that start with get or is. I don't know of way to do this, nor can I find anything matching this type of use case. I did see this post about using executeFunctionByName, but that didn't work on the App Script environment. If it did work, I'd have used something like this..

var allKeys =  Object.keys(aFile);
for(var i=0;i&lt;allKeys.length;i++){
 var eachProperty = allKeys[i];
 
 if(eachProperty.search(RegExp(&quot;^get|^is&quot;))&gt;-1 ){
  //??? run the literal name of function on aFile and print result?
    Logger.log(eachProperty + &#39;:&#39; + executeFunctionByName(eachProperty,aFile));
 }
}

If there's an entirely better approach to this that I'm unaware of, feel free to redirect me.

答案1

得分: 6

尽管我不确定我是否能正确理解您期望的结果,但以下示例脚本可能适用。

示例脚本:

function getObj_(file) {
  const exclude = ["getAccess", "getAs", "getSecurityUpdateEligible", "getSecurityUpdateEnabled"];
  const obj = {};
  for (let method in file) {
    if (!exclude.includes(method) && /^get|^is/.test(method)) {
      obj[method] = file[method]();
    }
  }
  return obj;
}

function sample1() {
  const file = DriveApp.getFileById("###fileId###");
  const res = getObj_(file);
  console.log(res)
}

function sample2() {
  const res = [];
  const files = DriveApp.getFolderById("###folderId###").getFiles();
  while (files.hasNext()) {
    res.push(getObj_(files.next()));
  }
  console.log(res)
}

测试:

运行此脚本时,将获得以下结果。 在此测试中,运行 sample1 到电子表格。 键是方法。 在这种情况下,类对象包含在值中。 请注意这一点。

运行 sample2 时,将以下对象包含在数组中。

{
  "getOwner": DriveUser,
  "getViewers": "",
  "getThumbnail": Blob,
  "getTargetId": null,
  "getDownloadUrl": null,
  "getTargetMimeType": null,
  "getTargetResourceKey": null,
  "getMimeType": "application/vnd.google-apps.spreadsheet",
  "getEditors": "",
  "getName": "filename",
  "getId": "###",
  "getSize": 12345,
  "isStarred": false,
  "getParents": FolderIterator,
  "getDescription": null,
  "isTrashed": false,
  "getUrl": "https://docs.google.com/spreadsheets/d/###/edit?usp=drivesdk",
  "getResourceKey": null,
  "getSharingAccess": "PRIVATE",
  "getSharingPermission": "NONE",
  "getDateCreated": Date,
  "getLastUpdated": Date,
  "getBlob": Blob,
  "isShareableByEditors": true
}

注意:

  • 我认为当使用 Drive API 如下所示时,也可以检索文件元数据。 但根据您的问题,我猜想您可能想使用 Drive 服务(DriveApp)而不是 Drive API。 因此,我提出了以上示例脚本。 如果您的目标包括使用 Drive API,则可能可以使用以下脚本。

    const obj = Drive.Files.list({ q: `'${folderId}' in parents and trashed=false`, fields: "*", corpora: "allDrives", includeItemsFromAllDrives: true, supportsAllDrives: true, maxResults: 1000 });
    

参考:

英文:

Although I'm not sure whether I could correctly understand your expected result, how about the following sample script?

Sample script:

function getObj_(file) {
  const exclude = [&quot;getAccess&quot;, &quot;getAs&quot;, &quot;getSecurityUpdateEligible&quot;, &quot;getSecurityUpdateEnabled&quot;];
  const obj = {};
  for (let method in file) {
    if (!exclude.includes(method) &amp;&amp; /^get|^is/.test(method)) {
      obj[method] = file[method]();
    }
  }
  return obj;
}

function sample1() {
  const file = DriveApp.getFileById(&quot;###fileId###&quot;);
  const res = getObj_(file);
  console.log(res)
}

function sample2() {
  const res = [];
  const files = DriveApp.getFolderById(&quot;###folderId###&quot;).getFiles();
  while (files.hasNext()) {
    res.push(getObj_(files.next()));
  }
  console.log(res)
}

Testing:

When this script is run, the following result is obtained. In this test, sample1 is run to Spreadsheet. The keys are the methods. In this case, the Class object is included in the values. Please be careful about this.

When sample2 is run, the following object is included in an array.

{
  &quot;getOwner&quot;: DriveUser,
  &quot;getViewers&quot;: &quot;&quot;,
  &quot;getThumbnail&quot;: Blob,
  &quot;getTargetId&quot;: null,
  &quot;getDownloadUrl&quot;: null,
  &quot;getTargetMimeType&quot;: null,
  &quot;getTargetResourceKey&quot;: null,
  &quot;getMimeType&quot;: &quot;application/vnd.google-apps.spreadsheet&quot;,
  &quot;getEditors&quot;: &quot;&quot;,
  &quot;getName&quot;: &quot;filename&quot;,
  &quot;getId&quot;: &quot;###&quot;,
  &quot;getSize&quot;: 12345,
  &quot;isStarred&quot;: false,
  &quot;getParents&quot;: FolderIterator,
  &quot;getDescription&quot;: null,
  &quot;isTrashed&quot;: false,
  &quot;getUrl&quot;: &quot;https://docs.google.com/spreadsheets/d/###/edit?usp=drivesdk&quot;,
  &quot;getResourceKey&quot;: null,
  &quot;getSharingAccess&quot;: &quot;PRIVATE&quot;,
  &quot;getSharingPermission&quot;: &quot;NONE&quot;,
  &quot;getDateCreated&quot;: Date,
  &quot;getLastUpdated&quot;: Date,
  &quot;getBlob&quot;: Blob,
  &quot;isShareableByEditors&quot;: true
}

Note:

  • I thought that when Drive API is used as follows, the file metadata can be also retrieved. But, from your question, I guessed that you might want to use the Drive service (DriveApp) instead of Drive API. So, I proposed the above sample script. If your goal includes using Drive API, the following script might be able to be used.

    const obj = Drive.Files.list({ q: `&#39;${folderId}&#39; in parents and trashed=false`, fields: &quot;*&quot;, corpora: &quot;allDrives&quot;, includeItemsFromAllDrives: true, supportsAllDrives: true, maxResults: 1000 });
    

Reference:

huangapple
  • 本文由 发表于 2023年5月14日 05:43:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定