借助高级云端硬盘服务,您可以在 Apps 脚本中使用 Google Drive API。与 Apps 脚本的内置云端硬盘服务非常相似,此 API 允许脚本在 Google 云端硬盘中创建、查找和修改文件和文件夹。在大多数情况下,内置服务更易于使用,但此高级服务提供了一些额外的功能,包括访问自定义文件属性以及文件和文件夹的修订版本。
参考
如需详细了解此服务,请参阅 Google Drive API 的参考文档。与 Apps 脚本中的所有高级服务一样,高级云端硬盘服务使用的对象、方法和参数均与公共 API 相同。如需了解详情,请参阅如何确定方法签名。
/** * Uploads a new file to the user's Drive. */functionuploadFile(){try{// Makes a request to fetch a URL.constimage=UrlFetchApp.fetch('http://goo.gl/nd7zjB').getBlob();letfile={name:'google_logo.png',mimeType:'image/png'};// Create a file in the user's Drive.file=Drive.Files.create(file,image,{'fields':'id,size'});console.log('ID:%s,Filesize(bytes):%s',file.id,file.size);}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedtouploadfilewitherror%s',err.message);}}
/** * Lists the top-level folders in the user's Drive. */functionlistRootFolders(){constquery='"root"inparentsandtrashed=falseand'+'mimeType="application/vnd.google-apps.folder"';letfolders;letpageToken=null;do{try{folders=Drive.Files.list({q:query,pageSize:100,pageToken:pageToken});if(!folders.files||folders.files.length===0){console.log('Allfoldersfound.');return;}for(leti=0;i < folders.files.length;i++){constfolder=folders.files[i];console.log('%s(ID:%s)',folder.name,folder.id);}pageToken=folders.nextPageToken;}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedwitherror%s',err.message);}}while(pageToken);}
/** * Lists the revisions of a given file. * @param {string} fileId The ID of the file to list revisions for. */functionlistRevisions(fileId){letrevisions;constpageToken=null;do{try{revisions=Drive.Revisions.list(fileId,{'fields':'revisions(modifiedTime,size),nextPageToken'});if(!revisions.revisions||revisions.revisions.length===0){console.log('Allrevisionsfound.');return;}for(leti=0;i < revisions.revisions.length;i++){constrevision=revisions.revisions[i];constdate=newDate(revision.modifiedTime);console.log('Date:%s,Filesize(bytes):%s',date.toLocaleString(),revision.size);}pageToken=revisions.nextPageToken;}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedwitherror%s',err.message);}}while(pageToken);}
/** * Adds a custom app property to a file. Unlike Apps Script's DocumentProperties, * Drive's custom file properties can be accessed outside of Apps Script and * by other applications; however, appProperties are only visible to the script. * @param {string} fileId The ID of the file to add the app property to. */functionaddAppProperty(fileId){try{letfile={'appProperties':{'department':'Sales'}};// Updates a file to add an app property.file=Drive.Files.update(file,fileId,null,{'fields':'id,appProperties'});console.log('ID:%s,appProperties:%s',file.id,JSON.stringify(file.appProperties,null,2));}catch(err){// TODO (developer) - Handle exceptionconsole.log('Failedwitherror%s',err.message);}}
[null,null,["最后更新时间 (UTC):2024-12-21。"],[[["The advanced Drive service in Apps Script provides more features than the built-in service, like access to custom file properties and revisions."],["This advanced service requires enabling before use and mirrors the functionality of the Google Drive API."],["Code samples demonstrate how to upload files, list folders and revisions, and add custom properties to files in Google Drive using this service."],["The provided samples utilize version 3 of the Google Drive API and illustrate common Drive operations within Apps Script."],["For comprehensive details, refer to the Google Drive API reference documentation and support guide."]]],[]]