这里的问题和答案非常有帮助: 使用 Google Apps 脚本为特定文件设置新的驱动器标签
我希望将其提升到一个新的水平,并在也具有选择的已知文件上设置标签。因此,就我而言,我有这些标签选择及其 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),但不太明白。谢谢。
我找不到有关设置选择的文档。我尝试提前输入,看看是否能出现一些有用的内容,但没有运气。令人惊讶的是,专门针对 GAS 的文档非常少。
经过一番尝试和错误,我找到了解决方案。 Apps 脚本的这些方法和参数没有详细记录。请注意,要获取字段 id 和字段选择 id,我必须获取具有感兴趣的字段和选择值的示例文件的标签 (
Drive.Files.listLabels(fileId)
),然后手动检查返回的对象以查找 fieldId
和 fieldValue
选择 ID。
这是适合我的解决方案:
var fileId = ""
var labelId = ""
var fieldId = ""
var fieldValue = ""
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("%s",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);
惠勒的回答非常有效。我最终大约在同一时间找到了这个解决方案,所以也将其发布在这里:
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);
这里有一些代码生成访问字段时使用的常量。使用这里描述的方法对我帮助很大
function outCode () {
var pageToken;
var output;
var labelSchema = [];
do {
const response = DriveLabels.Labels.list(
{
publishedOnly: true, pageToken: pageToken,
view: "LABEL_VIEW_FULL",
fields: "labels/name,labels/id,labels/revisionId,labels/properties(title,description),labels/fields(id,properties(*,displayName),textOptions,integerOptions,dateOptions,selectionOptions(listOptions(maxEntries),choices(id,properties(displayName,description))),userOptions)"
})
pageToken = response.nextPageToken;
labelSchema = labelSchema.concat(response.labels);
} while (pageToken);
console.log('Stored %d labels in schema; you can use the following output for programming', labelSchema.length);
console.log(JSON.stringify(labelSchema));
output = "const LabelIds = {";
if (labelSchema && labelSchema.length > 0) {
for (var i = 0; i < labelSchema.length; i++) {
output+= `$${labelSchema[i].properties.title.replace(/[^ßÜÖÄA-Z0-9]+/ig,'_')}: '${labelSchema[i].id}'`;
var fields = labelSchema[i].fields;
if (fields && fields.length > 0) {
output+= `,_${labelSchema[i].properties.title.replace(/[^ßÜÖÄA-Z0-9]+/ig,'_')}: {`;
for (var f = 0; f < fields.length; f++) {
output+= f>0?',':``;
output+= `$${fields[f].properties.displayName.replace(/[^ßÜÖÄA-Z0-9]+/ig,'_')}: '${fields[f].id}'`;
var choices = fields[f].selectionOptions?.choices;
if (choices && choices.length > 0) {
output+= `,_${fields[f].properties.displayName.replace(/[^ßÜÖÄA-Z0-9]+/ig,'_')}: {`;
for (var c = 0; c < choices.length; c++) {
output+= c>0?',':``;
output+= `$${choices[c].properties.displayName.replace(/[^ßÜÖÄA-Z0-9]+/ig,'_')+": '"+choices[c].id}'`;
}
output+= '}';
}
}
output+= '}'+((i<labelSchema.length-1)?',':'');
}
}
}
output+= `};`;
console.log('You can use the following output for programming:');
console.log(output)
}