【Google App Script GAS】 Google Drive Picker

 //code.gs

function doGet(e) {

  return HtmlService.createHtmlOutputFromFile('index.html').setTitle("Google Drive Picker");

}

function getOAuthToken() {

  DriveApp.getRootFolder();

  return ScriptApp.getOAuthToken();

}



//index.html

<!DOCTYPE html>

<html>

<head>

<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">

<script type="text/javascript">

var DIALOG_DIMENSIONS = {

width: 600,

height: 425

};

var pickerApiLoaded = false;


function onApiLoad() {

gapi.load('picker', {

'callback': function() {

pickerApiLoaded = true;

}

});

google.script.run.withSuccessHandler(createPicker)

.withFailureHandler(showError).getOAuthToken();

}


function createPicker(token) {


if (pickerApiLoaded && token) {


var docsView = new google.picker.DocsView()

.setIncludeFolders(true)

.setMimeTypes('application/vnd.google-apps.folder')

.setSelectFolderEnabled(true);

var uploadView = new google.picker.DocsUploadView();

var picker = new google.picker.PickerBuilder()

.addView(uploadView)

.enableFeature(google.picker.Feature.NAV_HIDDEN)

.hideTitleBar()

.setSize(DIALOG_DIMENSIONS.width - 2, DIALOG_DIMENSIONS.height - 2)

.setOAuthToken(token)

.setCallback(pickerCallback)

.setOrigin("https://script.google.com/macros/s/AKfycbyX3j89UILtsjOlj9QhOc13GFkSb2iFJF97sb4CDVnewq7fn2E/exec")

.build();


picker.setVisible(true);


} else {

showError('Unable to load the file picker.');

}

}


/**

* A callback function that extracts the chosen document's metadata from the

* response object. For details on the response object, see

* https://developers.google.com/picker/docs/result

*

* @param {object} data The response object.

*/

function pickerCallback(data) {

var action = data[google.picker.Response.ACTION];

if (action == google.picker.Action.PICKED) {

var doc = data[google.picker.Response.DOCUMENTS][0];

var id = doc[google.picker.Document.ID];

// Show the ID of the Google Drive folder

document.getElementById('result').innerHTML = id;

} else if (action == google.picker.Action.CANCEL) {

google.script.host.close();

}

}


function showError(message) {

document.getElementById('result').innerHTML = 'Error: ' + message;

}

</script>

</head>


<body>

<div>

<p id='result'></p>

</div>

<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>

</body>

</html>


No comments:

I Quit AeroMedLab

Watch now (2 mins) | Today is my last day at AeroMedLab ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­...

Contact Form

Name

Email *

Message *