【问题标题】:Extending a google spreadsheet into a google web app将谷歌电子表格扩展到谷歌网络应用程序
【发布时间】:2016-03-01 04:40:20
【问题描述】:

我在使用谷歌脚本进行编码时,遇到了一个困扰了几天的问题。当我从谷歌电子表格中调用数据以尝试将其显示在网页上时,我正在使用 Code.gs(在谷歌中创建网络应用程序的默认页面)。我在调用数据添加将其存储到数组中时没有问题,但现在我正在努力尝试将其返回到我的 javascript 代码中。可以做到这一点还是我可以做些什么来解决它?我的代码如下。

function getContents()
{
  var sheet = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1xum5t4a83CjoU4EfGd50f4ef885F00d0erAvUYX0JAU/edit#gid=0&vpid=A1');
  var range = sheet.getDataRange();
  var values = range.getValues();
  
  for (var i = 0; i < values.length; i++) {
      var education = [];
      for (var j = 0; j < values[i].length; j++) {
         if (values[i][j]) {
            if(j==1){
                education[education.length] = values[i][j];
            }
         }         
       }
     }
   Logger.log(education);
   return education;
 }

从那个 Code.gs 代码中,我希望它返回到一个 Javascript 函数,该函数说:

function onNew(){
    var input = google.script.run.getContents();
    for(var = 0; i<input.length; i++)
    {
       $("#main").append("<div id='class'>"+input[i]+"</div>);
    }
  }

每当我尝试运行它时,它都会说它会导致错误,因为它是未定义的。提前致谢!有什么帮助!

【问题讨论】:

    标签: javascript import google-apps-script webapp2


    【解决方案1】:

    您需要使用withSuccessHandler()。您的变量input 不会收到来自google.script.run.getContents() 的返回

    将客户端代码分成两个函数:

    HTML 脚本

    function onNew() {
      google.script.run
        .withSuccessHandler(appendEducationHTML)
        .getContents();
    };
    
    function appendEducationHTML(returnedInfo) {
      console.log('returnedInfo type is: ' + typeof returnedInfo);
      console.log('returnedInfo: ' + returnedInfo);
    
      //If return is a string.  Convert it back into an array
      //returnedInfo = returnedInfo.split(",");
    
      for (var = 0;i < returnedInfo.length; i++) {
        $("#main").append("<div id='class'>"+returnedInfo[i]+"</div>);
      };
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      相关资源
      最近更新 更多