简体   繁体   English

从Google脚本访问HTML脚本变量

[英]Accessing HTML script variable from google script

I'm trying to put together a picker in google sheets. 我正在尝试在Google表格中整理一个选择器。 Once a file has been uploaded to google drive, I want the url to be posted in the current cell in the spreadsheet. 将文件上传到Google驱动器后,我希望将网址发布到电子表格的当前单元格中。

This is my pickerCallback located in an html file: 这是我的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;
}

This returns the url in the picker dialog box. 这将在选择器对话框中返回URL。

My accessSpreadsheet function looks like this and is located in a google script file: 我的accessSpreadsheet函数如下所示,位于google脚本文件中:

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

The function runs but it cannot define message. 该函数运行,但无法定义消息。 Is there a way to access the variable defined in the function in the html file from the google scripts function? 有没有办法从Google脚本功能访问html文件中的函数中定义的变量? Or is there another better way to do this? 还是有另一种更好的方法来做到这一点?

Solution: 解:

Pass the string id from client side to server. 将字符串id从客户端传递到服务器。

Snippets: 摘录:

google.script.run.accessSpreadsheet(id); 

function accessSpreadsheet(id) {

 currentCell.setValue(id);

Reference: 参考:

Client-Server communication 客户端-服务器通信

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM