使用Google Apps Script将新的驱动器标签与所选文件设置为特定文件。

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

Setting new Drive Labels With Selections to a specific file using Google Apps Script

问题

这是您要翻译的内容:

这里的问题与回答非常有帮助:
https://stackoverflow.com/q/76256164/22038334

我希望将其提升到下一个级别,并在已知具有选择功能的文件上设置标签。所以在我的情况下,我有这些标签选择和它们的ID:

3ZB971298F:已归档

9745AD2361:未分类

我有

  var AddLabel = Drive.newModifyLabelsRequest()
    .setLabelModifications(
      Drive.newLabelModification()
        .setLabelId(labelId)
    );

这可以很好地设置由lableId定义的标签。但这还不够,因为它缺少选择功能。如何添加一个选择功能,假设"已归档"的ID如上所示。

我已经阅读了文档(https://developers.google.com/drive/api/reference/rest/v2/files/modifyLabels#FieldModification),但还不太明白。谢谢。

我找不到关于设置选择功能的文档。我尝试了输入提示,看看是否能找到有用的信息,但没有运气。令人惊讶的是,关于Google Apps Script的具体文档非常少。

英文:

The question & answer here was very helpful:
https://stackoverflow.com/q/76256164/22038334

I'm looking to take this to the next level, and set a label on a known file that also has Selection. So in my case, I have these label selections and their IDs:

3ZB971298F: Archived

9745AD2361: Unclassified

I have

  var AddLabel = Drive.newModifyLabelsRequest()
    .setLabelModifications(
      Drive.newLabelModification()
        .setLabelId(labelId)
    );

And this works well to set the label that's defined by lableId. But it's not complete, because it's missing the Selection. How can I add a selection, let's assume "Archived" with the ID as above.

I've read through the documentation (https://developers.google.com/drive/api/reference/rest/v2/files/modifyLabels#FieldModification), but not quite getting it. Thanks.

I couldn't find documentation on setting Selection. I tried the typeahead to see if something useful could come up, but no luck. There is surprisingly very little documentation specific to GAS.

答案1

得分: 1

我已翻译以下内容:

我在一些试验和错误后找到了解决方案。这些方法和参数在应用脚本中没有很好的文档支持。请注意,要获取字段 id 和字段选择 id,我不得不获取具有感兴趣字段和选择值的示例文件的标签(Drive.Files.listLabels(fileId))的标签,然后手动检查返回的对象以找到fieldIdfieldValue的 selectionId。

以下是对我有效的解决方案:

// language: lang-js
var fileId = ""
var labelId = ""
var fieldId = ""
var fieldValue = ""

Logger.log(DriveApp.getFileById(fileId).getName());

// 添加标签到文件(字段将为空)
var AddLabel = Drive.newModifyLabelsRequest()
AddLabel.setLabelModifications(Drive.newLabelModification().setLabelId(labelId));

// 提交
var output = Drive.Files.modifyLabels(AddLabel, fileId);
Logger.log("%s", output);

// 设置文件的字段
var fieldModification = Drive.newLabelFieldModification();
fieldModification.fieldId = fieldId;
fieldModification.setSetSelectionValues([fieldValue]);

var modifyLabelsRequest = Drive.newModifyLabelsRequest();
modifyLabelsRequest.labelModifications = Drive.newLabelModification()
modifyLabelsRequest.labelModifications.labelId = labelId;
modifyLabelsRequest.labelModifications.setFieldModifications([fieldModification]);

// 提交
var output = Drive.Files.modifyLabels(modifyLabelsRequest, fileId);
Logger.log(output);

如果您需要进一步的帮助,请随时提出。

英文:

I've figured out the solution after some trial and error. These methods and parameters are not well documented for Apps Scripts. Please note, to acquire the field id and the field selection id, I had to fetch the label of a sample file that has the field and selection value of interest (Drive.Files.listLabels(fileId)) and then manually inspect the returned object to find the fieldId and fieldValue selectionId.

Here is the solution that works for me:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var fileId = &quot;&quot;
var labelId = &quot;&quot;
var fieldId = &quot;&quot;
var fieldValue = &quot;&quot;

Logger.log(DriveApp.getFileById(fileId).getName());

// Add Label To The File (Fields will be empty)
var AddLabel =Drive.newModifyLabelsRequest()
AddLabel.setLabelModifications(Drive.newLabelModification().setLabelId(labelId));

// Commit
var output = Drive.Files.modifyLabels(AddLabel,fileId);
Logger.log(&quot;%s&quot;,output);

// Set field of file
var fieldModification = Drive.newLabelFieldModification();
fieldModification.fieldId = fieldId;
fieldModification.setSetSelectionValues([fieldValue]);

var modifyLabelsRequest = Drive.newModifyLabelsRequest();
modifyLabelsRequest.labelModifications = Drive.newLabelModification()
modifyLabelsRequest.labelModifications.labelId = labelId;
modifyLabelsRequest.labelModifications.setFieldModifications([fieldModification]);

// Commit
var output = Drive.Files.modifyLabels(modifyLabelsRequest,fileId);
Logger.log(output);

<!-- end snippet -->

答案2

得分: 1

Wyler的回答非常有效。我最终也找到了这个解决方案,所以也在这里发布:

 var labelId = "";
 var fieldId = "";
 var selectionId = "";

 const request = {
    "kind": "drive#modifyLabelsRequest",
    "labelModifications": [
      {
        "kind": "drive#labelModification",
        "labelId": labelId,
        "fieldModifications": [
          {
            "kind": "drive#labelFieldModification",
            "fieldId": fieldId,
            "setSelectionValues": [
              selectionId
            ],
            "unsetValues": false
          }
        ],
        "removeLabel": false
      }
    ]
  }
  var output = Drive.Files.modifyLabels(request, fileId);
  Logger.log('Label Modified output: ', output.modifiedLabels);
  Logger.log('File: ' + fileId + ' Label: ' + selectionName + '/' + selectionId);
英文:

Wyler's answer worked great. I ended up finding this solution at about the same time, so posting it here as well:

 var labelId = &quot;&quot;;
 var fieldId = &quot;&quot;;
 var selectionId = &quot;&quot;;

 const request = {
    &quot;kind&quot;: &quot;drive#modifyLabelsRequest&quot;,
    &quot;labelModifications&quot;: [
      {
        &quot;kind&quot;: &quot;drive#labelModification&quot;,
        &quot;labelId&quot;: labelId,
        &quot;fieldModifications&quot;: [
          {
            &quot;kind&quot;: &quot;drive#labelFieldModification&quot;,
            &quot;fieldId&quot;: fieldId,
            &quot;setSelectionValues&quot;: [
              selectionId
            ],
            &quot;unsetValues&quot;: false
          }
        ],
        &quot;removeLabel&quot;: false
      }
    ]
  }
  var output = Drive.Files.modifyLabels(request, fileId);
  Logger.log(&#39;Label Modified output: &#39;, output.modifiedLabels);
  Logger.log(&#39;File: &#39; + fileId + &#39; Label: &#39; + selectionName + &#39;/&#39; + selectionId);

huangapple
  • 本文由 发表于 2023年6月8日 04:36:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426947.html
匿名

发表评论

匿名网友

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

确定