/** * List labels available to the user. */functionlistLabels(){letpageToken=null;letlabels=[];do{try{constresponse=DriveLabels.Labels.list({publishedOnly:true,pageToken:pageToken,});pageToken=response.nextPageToken;labels=labels.concat(response.labels);}catch(err){// TODO (developer) - Handle exceptionconsole.log("Failed to list labels with error %s",err.message);}}while(pageToken!=null);console.log("Found %d labels",labels.length);}
获取标签
以下代码示例展示了如何通过单个标签的资源名称(即标签的字符串值)获取该标签。如需查找标签名称,请通过 API 获取标签列表,或使用 Google 云端硬盘标签管理器。如需详细了解标签管理器,请参阅管理云端硬盘标签。
/** * Get a label by name. * @param {string} labelName The label name. */functiongetLabel(labelName){try{constlabel=DriveLabels.Labels.get(labelName,{view:"LABEL_VIEW_FULL",});consttitle=label.properties.title;constfieldsLength=label.fields.length;console.log(`Fetched label with title: '${title}' and ${fieldsLength} fields.`,);}catch(err){// TODO (developer) - Handle exceptionconsole.log("Failed to get label with error %s",err.message);}}
/** * List Labels on a Drive Item * Fetches a Drive Item and prints all applied values along with their to their * human-readable names. * * @param {string} fileId The Drive File ID */functionlistLabelsOnDriveItem(fileId){try{constappliedLabels=Drive.Files.listLabels(fileId);console.log("%d label(s) are applied to this file",appliedLabels.labels.length,);for(constappliedLabelofappliedLabels.labels){// Resource name of the label at the applied revision.constlabelName=`labels/${appliedLabel.id}@${appliedLabel.revisionId}`;console.log("Fetching Label: %s",labelName);constlabel=DriveLabels.Labels.get(labelName,{view:"LABEL_VIEW_FULL",});console.log("Label Title: %s",label.properties.title);for(constfieldIdofObject.keys(appliedLabel.fields)){constfieldValue=appliedLabel.fields[fieldId];constfield=label.fields.find((f)=>f.id===fieldId);console.log(`Field ID: ${field.id}, Display Name: ${field.properties.displayName}`,);switch(fieldValue.valueType){case"text":console.log("Text: %s",fieldValue.text[0]);break;case"integer":console.log("Integer: %d",fieldValue.integer[0]);break;case"dateString":console.log("Date: %s",fieldValue.dateString[0]);break;case"user":{constuser=fieldValue.user.map((user)=>{return`${user.emailAddress}: ${user.displayName}`;}).join(", ");console.log(`User: ${user}`);break;}case"selection":{constchoices=fieldValue.selection.map((choiceId)=>{returnfield.selectionOptions.choices.find((choice)=>choice.id===choiceId,);});constselection=choices.map((choice)=>{return`${choice.id}: ${choice.properties.displayName}`;}).join(", ");console.log(`Selection: ${selection}`);break;}default:console.log("Unknown: %s",fieldValue.valueType);console.log(fieldValue.value);}}}}catch(err){// TODO (developer) - Handle exceptionconsole.log("Failed with error %s",err.message);}}