1

我构建了一个与 Google Sheet(s) 文件相关联的 Google Apps 脚本 (GAS) 应用程序。该应用程序有这些

Code.gs
Example.html

文件。这张图

在此处输入图像描述

显示了我要构建的文件保存功能的整体流程。在 Example.html 文件中,一个 HTML 控件调用 saveSheet() 函数

function saveSheet() {
    google.script.run.withFailureHandler(callBackFailure)
    .withSuccessHandler(getOAuthToken)
    .funcSaveSheet();
}

它立即调用 Code.gs funcSaveSheet() 函数,在这个问题中大部分被忽略以节省空间。

function funcSaveSheet() {

    .   .   .   .   .
    .   .   .   .   .
    .   .   .   .   .
    .   .   .   .   .

    var blob = response.getBlob().setName(localSpreadSheet.getName() + ' - ' +
        sheet.getName() + '.pdf');

    // This line shows that the function actually writes a file to Google Drive:

    folder.createFile(blob);
    finishedDoc = response.getContent();

    return finishedDoc;
}

funcSaveSheet() 函数在 Google Drive 中创建一个新文件。请注意,funcSaveSheet() 函数具有

folder.createFile(blob);

行(靠近底部)仅用于证明整个应用程序可以将文件写入 Google Drive。这一切都有效。现在,上面 saveSheet() 函数中的一行

google.script.run.withFailureHandler(callBackFailure)
.withSuccessHandler(getOAuthToken)
.funcSaveSheet();

接下来调用函数 getOAuthToken()

function getOAuthToken(docContentParam) {
    google.script.run.withFailureHandler(showError)
    .withSuccessHandler(createPicker)
    .getOAuthToken1(docContentParam);
  }

也位于 Example.html 文件中。这也有效。GAS 应用程序使用所有这些

google.script.run.withFailureHandler().withSuccessHandler().{函数调用}

行,因为 Google Drive Picker 最终需要来自多个 Code.gs 文件函数调用的多个“结果集”(此处未显示)。最后,getOAuthToken() 函数中的行调用位于 Example.html 中的 createPicker() 函数:

function createPicker(fileParam) {
    if (pickerApiLoaded && OAuthTokenVal) {
        var picker = new google.picker.PickerBuilder()
        .enableFeature(google.picker.Feature.NAV_HIDDEN)
        .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)

    // Use DocsUploadView to upload documents to Google Drive . . .

        .addView(new google.picker.DocsUploadView().setIncludeFolders(true))

    // In this example, the function sets these two values as global
    // variables, but in the actual GAS application, the engineering works
    // differently

        .setOAuthToken(OAuthTokenVal)
        .setDeveloperKey(DEVELOPER_KEY)
        .setCallback(pickerCallback)
        .setOrigin(google.script.host.origin)

    // This does not work
    // .setDocument(fileParam)

        .build();
        picker.setVisible(true);
    } else {
        showError('Unable to load the file picker.');
    }
}

我们来解决这个问题。createPicker() 函数接收由先前调用 funcSaveSheet() 构建的已完成的组装文件内容,作为 fileParam 参数。该函数构建了一个谷歌选择器,但正如这个屏幕截图所示

在此处输入图像描述

选择器不知道它应该将文件内容保存在 fileParam 参数的 / 中。选择器希望用户拖入或钻出现有文件。相反,我希望这个函数能够构建一个 Google Picker,它会自动“知道”它应该将文件保存为 / 通过 fileParam 参数传递给函数本身。我希望 Google Picker 克隆 Microsoft Word/Excel 新文件保存对话框的行为。

我大量搜索 - StackOverflow / Google Picker 参考/ 等等 - 但没有运气。在 Google Picker 参考中,我确实找到了

.setDocument(fileParam)

行,这里放置在 createPicker(fileParam) 函数的底部附近。我尝试了它,因为它看起来很有希望,尽管 Google Picker 参考资料并没有说明太多,而且谷歌搜索通常没有告诉我任何关于它的信息。这条线只完全杀死了选择器。如果有人可以指导我,我将不胜感激。如果我伸手去拿一座太远的桥,我也想知道这一点。

谢谢!

4

1 回答 1

0

我想出了一个解决办法。我没有使用 Google Picker,而是使用了Google Cloud Print。全面披露:

1) 谷歌云打印目前处于测试阶段

2)它只能将Google Drive根视为直接文件保存目标位置

但是,在云端硬盘中保存文件后,用户仍然可以直接在云端硬盘中移动该文件。

在Bit Vectors阅读有关此内容的更多信息。. .

于 2016-04-15T22:15:40.603 回答