从 google 脚本访问 HTML 脚本变量

Accessing HTML script variable from google script

我正在尝试在 google 张中组合一个选择器。 将文件上传到 google 驱动器后,我希望将 url 发布到电子表格的当前单元格中。

这是我的 pickerCallback,位于 html 文件中:

var message;

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 = 'https://drive.google.com/open?id=' + doc[google.picker.Document.ID];

        google.script.run.accessSpreadsheet(); 

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

这returns选择器对话框中的url。

我的 accessSpreadsheet 函数如下所示,位于 google 脚本文件中:

function accessSpreadsheet() {
     var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
     var currentCell = sheet.getCurrentCell();
     currentCell.setValue(message);
     return spreadsheet;
}

函数运行但无法定义消息。有没有办法从 google 脚本函数访问 html 文件中函数中定义的变量?或者还有其他更好的方法吗?

解决方案:

将字符串 id 从客户端传递到服务器。

片段:

google.script.run.accessSpreadsheet(id); 

function accessSpreadsheet(id) {

 currentCell.setValue(id);

参考:

Client-Server communication