从谷歌电子表格创建表格:如何操作数据

Creating Table from Google Spreadsheets: How to Manipulate Data?

本文关键字:操作 数据 何操作 电子表格 谷歌 创建 表格      更新时间:2023-09-26

目前,我正在做一个项目,从谷歌表单/电子表格拉数据,并有它出现在使用JQuery/数据表的网站上。我已经得到了帮助,使数据出现在网站上,但我遇到了一个新的问题。

上一个问题:是否可以创建一个公共数据库(电子表格)搜索与谷歌脚本?

在Google上,我有一个输出8列的表单:

    时间戳
  • 标题
  • 作者
  • li>主题(s)
  • 国家(s)
  • 标记

其中,我不需要出现时间戳,如果URL存在,我希望标题链接到URL。由于我不太熟悉Javascript或DataTables,我制作了第二张表格,我试图将其简化为以下六个:

  • 标题(目前使用URL格式为 <a> 标签)
  • 作者
  • 主题(s)
  • 国家(s)
  • 标记

这几乎可以工作,除了我用来构造表的脚本呈现Title字段,因为它在单元格中,<a href=""></a>在表中可见。目前情况见"实际产出"下的表格;"目标表外观"下的表是我的目标。

  • 电流输出:http://interculturalresources.weebly.com/webtest.html

  • 表创建脚本:http://www.weebly.com/uploads/1/7/5/3/17534471/tablescript.js

因此,是否有一种方法可以让<a href=""></a>作为一个链接,尽管被放在一起在谷歌电子表格单元格?或者,是否有一种方法来编辑当前脚本a)忽略时间戳列和b)使标题输出链接到URL从URL列的URL(与适当的条件)?


编辑:我现在专注于链接;我有一个解决方案的时间戳,其中涉及复制数据到一个新的电子表格(因为表单是严格的复制/粘贴信息)。当前的问题是让每个条目都有一个链接,假设URL在第一列,标题在第二列。请阅读Mogsdad的回答和我的第一条评论以获取更多信息。


解决方案:首先,我要感谢Mogsdad给了我灵感和洞察力的"火花",让我朝着正确的方向找到了解决方案。为了解释总体思路,我不想在目标网站上显示来自Google电子表格的一列(URL),而是使用其内容在另一列(Title)中创建链接。然后,一旦创建了表,就使用datattables对其进行格式化。表中的所有单元格必须包含某些内容,因此如果单元格为空白,则必须用"None"填充。

function cellEntries(json, dest, divId) {
    var table = document.createElement('table');
        table.setAttribute("id", divId + "table");                 //Assign ID to <table> from the <div> name.
    var thead = document.createElement('thead');
    var tbody = document.createElement('tbody');
    var thr;
    var tr;
    var entries = json.feed.entry;                                     
    var cols = json.feed.gs$colCount.$t;                                  //The number of columns in the sheet.
    var link;                                                           //Teporary holder for the URL of a row.
    for (var i=0; i <cols; i++) {                                       //For the first row of cells (column titles),
        var entry = json.feed.entry[i];
        if (entry.gs$cell.col == '1') {                                     //For First Column / URL Column, (1)
            if (thr != null) {
                tbody.appendChild(thr);
            }
            thr = document.createElement('tr');                                 //Create <thr>/<tr> (???).
        }
        else {                                                              //For all other columns,
            var th = document.createElement('th');                              
            th.appendChild(document.createTextNode(entry.content.$t));          //Create title for each column.
            thr.appendChild(th);
        }
    } 
    for (var i=cols; i < json.feed.entry.length; i++) {                 //For all remaining cells,
        var entry = json.feed.entry[i];
        if (entry.gs$cell.col == '1') {                                     //For First Column / URL Column, (1)
            if (tr != null) {
                tbody.appendChild(tr);
            }
            tr = document.createElement('tr');                                  //Create <tr>.
            hlink = entry.content.$t;                                           //Put URL content into hlink.
        }
        else if (entry.gs$cell.col == '2') {                                //For Title Column,(2)
            var td = document.createElement('td');
            if (hlink != "None") {                                              //If there is a link,
                var alink = document.createElement('a');                            //Make <a>
                alink.appendChild(document.createTextNode(entry.content.$t));       //Put content in <a>
                alink.setAttribute('href',hlink);                                   //Assign URL to <a>.
                td.appendChild(alink);                                              //Put <a> in <td>.
            }
            else {                                                              //If there is no link,
                td.appendChild(document.createTextNode(entry.content.$t));          //Put content in <td>.
            }
            tr.appendChild(td);
        }
        else {                                                              //For all other columns,
            var td = document.createElement('td');
            if (entry.content.$t != "None") {                                   //If content is not "None",
                td.appendChild(document.createTextNode(entry.content.$t));          //Output the content.
            }
            else {                                                              //Else,
                td.appendChild(document.createTextNode(""));                        //Output a blank cell.
            }
            tr.appendChild(td);
        }       
    } 
    $(thead).append(thr);
    $(tbody).append(tr);
    $(table).append(thead);
    $(table).append(tbody);
    $(dest).append(table);
    $(dest + "table").dataTable();
};
function importGSS(json){
   var divId = "targetdivid"                            //ID of the target <div>.
   cellEntries(json, "#" + divId, divId);
};

围绕tablescript.js:

    var th = document.createElement('th');
    th.appendChild(document.createTextNode(entry.content.$t));
>>>>
    thr.appendChild(th)

你可以这样添加一个超链接:

th.setAttribute('href',<link>);

<link>设置为特定出版物的超链接。

要做到这一点,您可以修改电子表格源,使链接在一列中,标题在另一列中。然后修改tablescript.js以组合链接和文本,如下所示:

var hlink = null;  // Temporary storage for hyperlink 
for (var i=0; i <cols; i++) {
    var entry = json.feed.entry[i];
    if (entry.gs$cell.col == '1') { 
        if (thr != null) {
            tbody.appendChild(thr);
        }
        thr = document.createElement('tr');
    }
    // Element 0 assumed to have hyperlink
    if (i == 0) {
       hlink = entry.content.$t;
    }
    else {
       var th = document.createElement('th');
       // If we have an hlink, set the href attribute.
       if (hlink !== null) {
          th.setAttribute('href',hlink);
          hlink = null;
       }
       th.appendChild(document.createTextNode(entry.content.$t));
       thr.appendChild(th);
   }
}