您可以在進階雲端硬碟服務中使用 Google Drive API 的 Apps Script。非常喜歡
Apps Script 內建雲端硬碟
服務,這個 API 可讓指令碼建立、尋找並
以及修改 Google 雲端硬碟中的檔案和資料夾。在大多數情況下
服務更加易於使用,不過這項進階服務還提供一些額外的
功能,包括存取自訂檔案屬性,以及對
檔案與資料夾
參考資料
如要進一步瞭解這項服務,請參閱 Google Drive API 的參考文件。與 Apps Script 中的所有進階服務一樣,進階 Google 雲端硬碟服務會使用與公開 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);}}
列出資料夾
以下程式碼範例說明如何列出使用者 Google 雲端硬碟中的頂層資料夾。請注意,使用網頁符記存取
也就是預測結果
/** * 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);}}