我需要將過濾后的電子表格資料發送到 html。使用以下代碼過濾資料。
function getAllData() {
var values = getByName('user3')
Logger.log(values)
return values
// DataTable.html calling this function with create table. But Data not showing in webApp deploy
}
function getByName(user, colName = 'userID') {
const sheet = SpreadsheetApp.getActiveSheet();
const data = sheet.getDataRange().getValues();
const hA = data.shift();
const idx = hA.reduce((a, h, i) => (a[h] = i, a), {});
let o = data.map(r => {
if (r[idx[colName]] == user) {
return r;
}
}).filter(r => r );
if (o && o.length > 0) {
return o;
}
}
我的 html 資料表代碼如下
function createTable(dataArray) {
$('#btn-close').click()
$(document).ready(function(){
$('#dataTable').DataTable({
//data: dataArray,
data: dataArray.slice(1),
columns: [
{ title: "Productname" },
{ title: "Price1" },
{ title: "Price2" },
{ title: "Price3" },
{ title: "Price4" },
{ title: "userID" },
{ title: "Edit", "render": function (data,type){
return "<button type='button' class='btn btn-outline-warning btn-xs editBtn' data-toggle='modal' data-target='#myModal' onclick='editData(this);'><i class='fa fa-edit'></i></button>";
}
},
{ title: "Delete", "render": function (data,type){
return "<button type='button' class='btn btn-outline-danger btn-xs deleteBtn' onclick='deleteData(this);'><i class='fa fa-trash''></i></button>";
}
},
],
// "ordering": false,
destroy:true,
responsive:true,
lengthMenu: [
[10, 15, 25, 50, 100, -1 ],
['10', '15', '25', '50','100', 'All' ]
],
order: [[2, "asc"], [2, "asc"], ],
});
});
}
電子表格鏈接
資料截圖圖片
以上資料過濾是絕對正確的。問題是標題不可用。因此 DATATABLE.html 不起作用。
我嘗試使用以下代碼。但我嘗試在上面的函式 getByName 中替換return r和return o
const headers=data.shift()
return { data:data,headers: headers}
但我得到代碼錯誤。
日志報告原件
記錄錯誤添加標題
uj5u.com熱心網友回復:
One way to fix this is by inserting the headers after the filtering by using Array.prototype.unshift
function getByName(user, colName = 'userID') {
const sheet = SpreadsheetApp.getActiveSheet();
const data = sheet.getDataRange().getValues();
const hA = data.shift();
const idx = hA.reduce((a, h, i) => (a[h] = i, a), {});
let o = data.map(r => {
if (r[idx[colName]] == user) {
return r;
}
}).filter(r => r );
if (o && o.length > 0) {
o.unshift(hA); // < ------------
return o;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/512525.html
標籤:谷歌应用脚本