无法返回数组[Google apps脚本] [英] Unable to return Array [Google apps script]

查看:83
本文介绍了无法返回数组[Google apps脚本]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有问题.我们现在拥有的代码可以使用,但是我们无法使数组返回到html页面.我们从html页面发送一个数字,然后发生returnData函数.进行时,它会通过loopsheetGewoon,然后返回一个数组.数组返回returnData,函数以数组的返回值结束.

We have a problem. The code we have now works, but we can't get the array to return on the html page. We are sending a number from the html page, and then the returnData function occurs. While it goes, it goes through loopsheetGewoon, and then it returns an array. The array is returned to returnData, and the function ends with the returnal of the array.

问题是,当我们尝试将数组获取到html页面以便可以从电子表格中获取单个数据时,它返回未定义的数据.我们还尝试了将JavaScript值传递给Google Apps脚本代码,但这没用.

The problem is, that when we try to get the array to the html page so we can get individual data from the spreadsheet, it returns undefined. We have also tried passinga javascript value to google apps script code, but that didn't work.

function loopSheetGewoon(data, studentNr){
    var SpreadSheetKeyA = "1h8fDwCUUPHRdmTHu-5gMyqU294ENZxCZcHCNCuN6r_Y";
    var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getActiveSheet();
    var array = [];
    for (var y = 0; y < data.length; ++y ) {
        var datum = Utilities.formatDate(data[y][0], "CET", "dd-MM-yyyy hh:mm");
        var nummer = data[y][2];
        if(studentNr.equals(nummer)){
            for (var x = 0; x < 35; x++ ) {
                array.push(data[y][x]);
            }
            return array;        
        }    
    }
}

下面是启动的功能.

function returnData(stuNr){
    var SpreadSheetKeyA = "1h8fDwCUUPHRdmTHu-5gMyqU294ENZxCZcHCNCuN6r_Y";
    var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getActiveSheet();
    var data = sheet1.getDataRange().getValues();
    var array = [];
    array = loopSheetGewoon(data, stuNr);
    Logger.log(array);
    return array;
}

下面是我们从html页面运行的功能.单击一个按钮时,它将一个数字发送到returnData函数(该函数存储为字符串值).现在,它应该用数组填充变量d,但是它始终返回未定义的值.

Below is the function we are running from the html page. When a button is clicked, it sends a number to the returnData function ( Which is stored as a string value). It should now fill up the variable d with the array, however it keeps returning undefined.

$("#oph").click(function ophalen(){
    var s = $("#nummer").val();
    var displayEl = document.getElementById("nummer");
    var d = [];
    alert(s);
    d = google.script.run.returnData(s);

    //for( var y = 0; y < d.length(); ++y){
        //var naam = data[y][1];
        //var studentnr = data[y][2];
        //var document = data[y][32];
    //}
    alert(d);
    var div = document.getElementById('block');
    div.innerHTML = naam;
});

所以我不确定我们在做什么错,因为该函数本身可以正常工作,直到返回html页面为止.

So I am not sure what we are doing wrong, since the function itself works fine, up to the point until it returns to the html page.

推荐答案

不幸的是,已记录的google.script.run限制之一(自2019年3月起仍然有效)是,您不能在任何方式,包括作为数组的一部分.

Unfortunately, one of the documented limitations of google.script.run (still valid as of March 2019) is that you can't pass a Date object in any way, including as part of an array.

您可以在某个范围内调用getDisplayValues()而不是getValues()来仅获取一个以字符串开头的数组,也可以通过处理getValues()数组将Date对象转换为String.

You can either call getDisplayValues() instead of getValues() on a range to only fetch an array of Strings to begin with, or you can convert Date objects to Strings by processing the getValues() array.

这篇关于无法返回数组[Google apps脚本]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆