谷歌应用程序脚本,谷歌选择器团队驱动器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了谷歌应用程序脚本,谷歌选择器团队驱动器相关的知识,希望对你有一定的参考价值。

221/5000

你好!目前我正在尝试谷歌团队驱动谷歌pikker。此代码适用于我的Google驱动器,但如果您将此代码用于Google Team Drives,则添加参数

.enableFeature(google.picker.Feature.SUPPORT_TEAM_DRIVES)

.setSelectFolderEnabled(true)

在文件显示窗口中为空。

我该怎么办呢?谢谢!

它看起来应该是这样的:

我认为OAuth 2.0存在问题。

关于团队驱动器https://developers.google.com/drive/v3/web/picker的选择器有很好的文章,但是没有使用这个参数:

.enableFeature(google.picker.Feature.SUPPORT_TEAM_DRIVES)

.setSelectFolderEnabled(true)

Picker.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)
                .setEnableTeamDrives(true);

            var picker = new google.picker.PickerBuilder()
                .addView(docsView)
                .enableFeature(google.picker.Feature.SUPPORT_TEAM_DRIVES)
                .enableFeature(google.picker.Feature.NAV_HIDDEN)
                .hideTitleBar()
                .setSize(DIALOG_DIMENSIONS.width - 2, DIALOG_DIMENSIONS.height - 2)
                .setOAuthToken(token)
                .setCallback(pickerCallback)
                .setOrigin('https://docs.google.com')
                .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;
            /**/

            google.script.run.searchFileInFoder(id);
            google.script.host.close();
            /**/
        } 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>

code.公司

  /*Picker for сhoose folder*/
    function showPicker() {
    var html = HtmlService.createHtmlOutputFromFile('Picker.html')
      .setWidth(600)
      .setHeight(425)
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
    SpreadsheetApp.getUi().showModalDialog(html, 'Select Folder');
    }

       function getOAuthToken() {
      DriveApp.getRootFolder();
      return ScriptApp.getOAuthToken();
    }
答案

更改你的docsView变量是这样定义的,所以你在DocsView的括号内包含google.picker.viewId.DOCS,如下所示:

let docsView = new google.picker.DocsView(google.picker.ViewId.DOCS)
                        .setEnableTeamDrives(true);

我试图在上面的问题中实现代码,我有几个控制台错误,并且选择器无法正常工作。可能它与.setOrigin参数有关。我不能说你的代码的哪一部分导致错误,因为我不知道你的控制台日志。但无论如何,下面是我的完整createPicker()函数,它适用于我:

function createPicker() {
    if (pickerApiLoaded && oauthToken) {
      var view = new google.picker.DocsView(google.picker.ViewId.FOLDERS)
                        .setSelectFolderEnabled(true);
      var picker = new google.picker.PickerBuilder()
          .enableFeature(google.picker.Feature.SUPPORT_TEAM_DRIVES)
          .addView(new google.picker.DocsView(google.picker.ViewId.DOCS)
                        .setEnableTeamDrives(true))
          .addView(google.picker.ViewId.DOCS)
          .addView(view)
          .setOAuthToken(oauthToken)
          .setDeveloperKey(developerKey)
          .setCallback(pickerCallback)
          .build();
      picker.setVisible(true);
    }
  }
另一答案

可能为时已晚,无法回答这个问题,但这是使用您的代码的解决方案。

    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 Teamdrivesview = new google.picker.DocsView()
                //.setIncludeFolders(true)
                .setMimeTypes('application/vnd.google-apps.folder')
                .setSelectFolderEnabled(true)
                .setEnableTeamDrives(true); 
            
            var docsView = new google.picker.DocsView()
                .setIncludeFolders(true)
                .setMimeTypes('application/vnd.google-apps.folder')
                .setSelectFolderEnabled(true)
                .setEnableTeamDrives(true);

            var picker = new google.picker.PickerBuilder()
                .addView(docsView)
                .addView(Teamdrivesview)
                .enableFeature(google.picker.Feature.SUPPORT_TEAM_DRIVES)
                .enableFeature(google.picker.Feature.NAV_HIDDEN)
                .hideTitleBar()
                .setSize(DIALOG_DIMENSIONS.width - 2, DIALOG_DIMENSIONS.height - 2)
                .setOAuthToken(token)
                .setCallback(pickerCallback)
                .setOrigin('https://docs.google.com')
                .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;
            /**/

            google.script.run.searchFileInFoder(id);
            google.script.host.close();
            /**/
        } else if (action == google.picker.Action.CANCEL) {
            google.script.host.close();
        }
    }

    function showError(message) {
        document.getElementById('result').innerHTML = 'Error: ' + message;
    }

以上是关于谷歌应用程序脚本,谷歌选择器团队驱动器的主要内容,如果未能解决你的问题,请参考以下文章

获取选择选择器意图谷歌驱动器文件文件没有谷歌驱动器Api集成Android

将谷歌驱动器选择器文件转换为 javascript 文件类型

我是不是需要为以下谷歌驱动器范围提交验证请求才能从选择器下载文件?

URL启动器与谷歌地图位置选择器冲突

如何在android中从谷歌驱动器中选择文件?

复制谷歌应用脚本文件,使用... 谷歌应用脚本到teamDrive中。