从Google电子表格创建表格:如何操作数据?
目前,我正在开发一个项目,用于从Google Form/Spreadsheet中提取数据,并使用JQuery/DataTables将其显示在网站上。我在获得数据出现在网站方面获得了帮助,但我遇到了一个新问题。从Google电子表格创建表格:如何操作数据?
前一题:Is it possible to create a public database (spreadsheet) search with Google Scripts?
在谷歌,我有输出八列一个表格:
- 时间戳
- 标题
- 作者
- 类型
- URL
- 主题
- 国家(或多个)
- 产品关键词
这些,我不需要的时间戳出现,我想标题,如果存在URL链接到的URL。因为我不是很流利的用JavaScript或数据表,我做了,我试图简化它归结为以下六个第二片:使用URL
- 标题(目前正在格式化为
<a>
标签) - 作者
- 类型
- 各种话题
- 国家(或多个)
- 产品关键词
除了我用来构造表格的脚本呈现标题字段,因为它在表格中可见的<a href=""></a>
的单元格中。对于当前情况,请参见“实际产出”标题下的表格; “目标表外观”下的表就是我的目标。
因此,有没有办法有<a href=""></a>
为纽带,给予尽管被放在一起在谷歌电子表格单元格?或者,有没有办法编辑当前脚本,以a)忽略时间戳列和b)使标题输出链接到URL列(具有适当的条件)?
编辑:我专注于现在的联系;我有一个时间戳的解决方案,涉及将数据复制到新的电子表格(因为表单对复制/粘贴信息严格)。目前的问题是让每个条目都有一个链接,假设URL在第一列,标题在第二列。请阅读Mogsdad的回答和我的第一条评论以获取更多信息。
解决方案:首先,我感谢Mogsdad为害得我在朝着解决的正确方向的灵感和洞察力的“火花”。为了解释总体思路,我不想在目标网站上显示来自Google Spreadsheet的一列(URL),而是使用它的内容在另一个(标题)中创建链接。然后,一旦表格被创建,DataTables被用来格式化它。表格中的所有单元格必须包含某些内容,因此如果单元格为空白,则必须填写“无”。
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
:与<link>
集
th.setAttribute('href',<link>);
...的超级链接为特定的出版物:
var th = document.createElement('th');
th.appendChild(document.createTextNode(entry.content.$t));
>>>>
thr.appendChild(th)
您可以通过这样添加超链接。
为了完成这项工作,您可以重新制作电子表格源,将链接放在一列中,将标题放在另一列中。然后修改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);
}
}
感谢这个,它肯定把我在正确的方向,但我有两个问题。首先,表格的单元格是否可以具有“href”属性?我编辑我的内部使用'a'元素,因为我对前者没有好运。其次,我希望将它应用于列表中的所有条目,而不仅仅是标题,所以上面的代码结构应该放在'td' /'tr'部分。我的问题是,我似乎无法理解在该部分中执行此操作的逻辑。任何见解? – 2013-04-23 23:42:13
对于替代品,请尝试查看[this](http://*.com/questions/7744714/jquery-adding-link-to-a-table-cell)或[this](http://*.com /问题/ 3207413 /表各单元链接到包含URL的列和行的数据)。有很多超链接单元格的例子,所以它应该是找到正确的魔法咒语让它为你工作的问题。 – Mogsdad 2013-04-24 00:54:01