I used JS to parse the csv file.
Example of csv :
Year,Model,Mileage,Color,Vin,Image
2012,BUICK LACROSSE FWD V6 FFV 4D SEDAN LEATHER,113046,BROWN,1G4GC5E34CF177752,https:imagelink
And the code of parser
$(document).ready(function(){
$('#load_data').click(function(){
$.ajax({
url:"Cars.csv",
dataType:"text",
success:function(data)
{
var cars_data = data.split(/\r?\n|\r/);
var table_data = '<table class="table table-bordered table-striped">';
for(var count = 0; count<cars_data.length; count++)
{
var cell_data = cars_data[count].split(",");
table_data += '<tr>';
for(var cell_count=0; cell_count<cell_data.length; cell_count++)
{
if(count === 0)
{
table_data += '<th>'+cell_data[cell_count]+'</th>';
}
else
{
table_data += '<td>'+cell_data[cell_count]+'</td>';
}
}
table_data += '</tr>';
}
table_data += '</table>';
$('#cars_table').html(table_data);
}
});
});
});
Example of select boxes
<select>
<option value="0">Model</option>
<option value="1">HERE COMES THE MODEL FROM PARSED CSV</option>
</select>
So how can i put data from this parsed csv to select boxes on the html ?
Since your question wasn't clear to me, I added two solutions. The first solution one takes the Model column and offers a selectbox for this column. You can change the column to use as a select box if you change the select_column_index (to use the Year for example, set select_column_index = 0).
I created the addTable(data) function. This is equal to your success:function(){...}, but for testing I cannot use ajax. Your code is then
$(document).ready(function(){
$('#load_data').click(function(){
$.ajax({
url:"Cars.csv",
dataType:"text",
success:addTable,
});
);
});
The code takes the column with the select_column_index and uses the first cell (in the first row in the select_column_index-th column) as the label. The following cells (in each row in the select_column_index-th column) are the options for the select.
Note that one should not use the label as the first option from the select box as you did in your example. This makes it possible for users to select the mode Model which makes no sense. This is more complicated for users and leads to mistakes. Also you have to check the result on the server side which is more unnecessary code.
You can check the output in the following snippet. The second solution is added below.
// for demo only
let csv =
"Year,Model,Mileage,Color,Vin,Image\n" +
"2012,BUICK LACROSSE FWD V6 FFV 4D SEDAN LEATHER,113046,BROWN,1G4GC5E34CF177752,\n" +
"2013,Lincoln MKZ 3.7L AWD,113046,Lincoln,,\n";
// for demo only
$(document).ready(function(){
addTable(csv);
});
function addTable(data){
var cars_data = data.split(/\r?\n|\r/);
var table_data = '<table class="table table-bordered table-striped">';
// use model for the select
var select_column_index = 1;
var label_data = "<label for='csv-select'>";
var select_data = "<select id='csv-select'>";
for(var count = 0; count<cars_data.length; count++){
var cell_data = cars_data[count].split(",");
table_data += '<tr>';
for(var cell_count=0; cell_count<cell_data.length; cell_count++){
if(count === 0){
table_data += '<th>'+cell_data[cell_count]+'</th>';
}
else{
table_data += '<td>'+cell_data[cell_count]+'</td>';
}
if(cell_count == select_column_index){
if(count == 0){
label_data += cell_data[cell_count];
}
else{
select_data +=
"<option value='" + cell_data[cell_count] + "'>" +
cell_data[cell_count] +
"</option>";
}
}
}
table_data += '</tr>';
}
table_data += '</table>';
label_data += "</label>";
select_data += "</select>";
$('#cars_table').html(table_data);
$('#select-wrapper').html(label_data + " " + select_data);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="cars_table"></div>
<div id="select-wrapper"></div>
The second solution takes all the columns and adds a select box for each column. Note that I did not add the table, you can just use your code.
This is basically the same code but I am saving the options to another variable. After all options of all columns are present, they are pasted (replace the %options%) in the select boxes.
// for demo only
let csv =
"Year,Model,Mileage,Color,Vin,Image\n" +
"2012,BUICK LACROSSE FWD V6 FFV 4D SEDAN LEATHER,113046,BROWN,1G4GC5E34CF177752,\n" +
"2013,Lincoln MKZ 3.7L AWD,113046,Lincoln,,\n";
// for demo only
$(document).ready(function(){
addSelects(csv);
});
function addSelects(data){
var cars_data = data.split(/\r?\n|\r/);
var select_options = [];
var select_html = "";
for(var count = 0; count < cars_data.length; count++){
var cell_data = cars_data[count].split(",");
for(var cell_count = 0; cell_count < cell_data.length; cell_count++){
if(count == 0){
select_html +=
"<div class='select-line'>" +
"<label for='select-" + cell_count + "'>" +
cell_data[cell_count] +
"</label>" +
"<select id='select-" + cell_count + "' name='" + cell_data[cell_count] + "'>" +
"%options%" +
"</select>" +
"</div>";
}
else{
if(select_options.length < cell_count){
select_options.push("");
}
select_options[cell_count] +=
"<option value='" + cell_data[cell_count] + "'>" +
cell_data[cell_count] +
"</option>";
}
}
}
for(var i = 0; i < select_options.length; i++){
select_html = select_html.replace("%options%", select_options[i]);
}
$('#selects').html(select_html);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="selects"></div>
Related
Here is my script for displaying loading data onto my table:
function my_data(result,j){
console.log(result);
var length = result.length;
console.log(length);
j = Number(j);
$('#news_data tbody').empty();
for(var i = 0 ; i < length; i++){
j+=1;
var tr = "<tr>";
tr += "<td>"+ j +"</td>";
tr += "<td><a href='"+ result[i].product_id +"' target='_blank'>"
+result[i].product_name +"</a></td>";
tr += "<td>"+ result[i].product_price +"</td>";
tr += "</tr>";
$('#news_data tbody').append(tr);
}
}
And here is my search:
$('#search_text').keyup(function () {
var search = $(this).val();
if (search != '') {
my_data(search);
} else {
my_data();
}
});
When I search I get the list of items filtered as undefined
Hi this is my sheet: https://docs.google.com/spreadsheets/d/1xkTt_1kLyROkUFComi8-NiKyDMan6617C1dA5UqWTSI/edit?usp=sharing
I want to share range A2:F8 in email using google script.
As far as I can say this script works fine:
function sendMail(){
var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sh.getRange("A2:O38").getValues();
//var htmltable =[];
var TABLEFORMAT = 'cellspacing="2" cellpadding="2" dir="ltr" border="1" style="width:100%;table-layout:fixed;font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:1px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:center;text-decoration:none;font-style:normal;'
var htmltable = '<table ' + TABLEFORMAT +' ">';
for (row = 0; row<data.length; row++){
htmltable += '<tr>';
for (col = 0 ;col<data[row].length; col++){
if (data[row][col] === "" || 0) {htmltable += '<td>' + 'None' + '</td>';}
else
if (row === 0) {
htmltable += '<th>' + data[row][col] + '</th>';
}
else {htmltable += '<td>' + data[row][col] + '</td>';}
}
htmltable += '</tr>';
}
htmltable += '</table>';
Logger.log(data);
Logger.log(htmltable);
MailApp.sendEmail("myemail#gmail.com", 'Daily report','' ,{htmlBody: htmltable})
}
The problem is when i get mail the merged cells are not in merged state.
This result in 3 columns instead of 1.
Can someone please tweak the code so that I get exactly like i formatted sheets.
I modified the code a little because I didn't want to send any emails. And if you'll move the table up to the top left corner and use sh.getDataRange() then you'll get the entire table with no problems.
This is my modified version.
function sendSomething(){
var sh = SpreadsheetApp.getActiveSheet();
var dataA = sh.getDataRange().getValues();
var None = ' ';
var htmltable ='';
var TABLEFORMAT = 'cellspacing="2" cellpadding="2" dir="ltr" border="1" style="width:100%;table-layout:fixed;font-size:10pt;font-family:arial,sans,sans-serif;border-collapse:collapse;border:1px solid #ccc;font-weight:normal;color:black;background-color:white;text-align:center;text-decoration:none;font-style:normal;'
var htmltable = '<table ' + TABLEFORMAT +' ">';
for (row = 0; row<dataA.length; row++)
{
htmltable += '<tr>';
for(col = 0 ;col<dataA[row].length; col++)
{
if (dataA[row][col] === "" || 0)
{
htmltable += '<td>' + None + '</td>';
}
else
if (row === 0)
{
htmltable += '<th>' + dataA[row][col] + '</th>';
}
else
{
htmltable += '<td>' + dataA[row][col] + '</td>';
}
}
htmltable += '</tr>';
}
htmltable += '</table>';
dispStatus('HTML',htmltable,800,500);
}
And this is my display routine which I incorporated into your code. It's just my version of the logger.
function dispStatus(title,html,width,height,modal)
{
var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
var width = typeof(width) !== 'undefined' ? width : 400;
var height = typeof(height) !== 'undefined' ? height : 300;
var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
var modal = typeof(modal) !== 'undefined' ? modal : false;
var htmlOutput = HtmlService
.createHtmlOutput(html)
.setWidth(width)
.setHeight(height);
if(!modal)
{
SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
}
else
{
SpreadsheetApp.getUi().showModalDialog(htmlOutput, title);
}
}
You still need to format the dates. Perhaps you can use Utilities and if you want to merge cells together in the header you'll need to figure out which ones to give a colspan="3".
I'm having a bit of trouble trying to get my json data response shown in a two column table. I've found lots of posts that show how to show it as a single row, but nothing as a two column table.
This is what I have so far and it is still only showing a single column with multiple rows:
var trHTML = '';
$.each(data.Titles, function (i, item) {
var v = 0;
trHTML += '<tr>';
if(v <= 2){
trHTML += '<td>' + data.Titles[i] + '<br><img src="' + data.Images[i] + '"></td>';
}
else{
var v = 0;
trHTML += '</tr>';
trHTML += '<tr>';
}
trHTML += '</tr>';
v++
});
$('#location').append(trHTML);
},
Try this:
var trHTML = '';
var v = 0;
$.each(data.Titles, function (i, item) {
// it keeps v always 0 or 1 regarding if it's first or second column
if(v >= 2){
v = 0;
}
if(v == 0){
trHTML += '<tr>';
}
trHTML += '<td>' + data.Titles[i] + '<br><img src="' + data.Images[i] + '"></td>';
if(v == 1){
trHTML += '</tr>';
}
v++
});
if(v == 1){
trHTML += '</tr>';
}
$('#location').append(trHTML);
},
I want to build an HTML table string for every 10 items in a Javascript array, and if there are less than 10 items in the last iteration I want to fill it with empty rows.
How do I achieve this?
var array_of_items; //array of items
//make a html table string every 10 items
var table = '<table>';
table += '<tr><td>' + item1 + '</td></tr>';
table += '<tr><td>' + item2 + '</td></tr>';
.
.
.
table += '<tr><td>' + item10 + '</td></tr>';
table += '</table>';
//make second table
table = '<table>';
table += '<tr><td>' + item11 + '</td></tr>';
table += '<tr><td>' + item12 + '</td></tr>'; //array ends here
table += '<tr><td></td></tr>';
.
.
.
table += '<tr><td></td></tr>';
table += '</table>';
$('#table_div').html(table);
var table = '';
var numTables = Math.ceil(array_of_items.length / 10); //determine how many tables
for (var i=0;i<numTables;i++){
table +='<table>';
for (var j=0;j<10;j++){
(array_of_items[i*10+j] ? table +='<tr><td>' + array_of_items[i*10+j] + '</td></tr>' : table +='<tr><td></td></tr>');
}
table +='</table>';
}
var ln = items.length;
var lnten = ln + 10 - ln%10;
var container = $('#table_div');
for (var j = 0; j < lnten; j++) {
var tbl;
if (j % 10 == 0)
tbl = $('<table/>');
var tr = $('<tr/>');
var td = $('<td/>');
if (j < ln)
td.text(items[j]);
tr.append(td);
tbl.append(tr);
if (j % 10 == 0)
container.append(tbl);
}
jsfiddle DEMO
I think you're looking for this
for (var i = 0; i < array_of_items.length; i+=10) {
var table = '<table>';
for (var j = i; j < i+10; ++j) {
if (array_of_items[j] === 'undefined') {
table += '<tr><td></td></tr>';
}
else table += '<tr><td>' + array_of_items[j] + '</td></tr>';
}
table += '</table>';
}
i have a board that contains 9 small boards and each small board contains 9 cells (Ultimate TicTacToe).
i'm trying to use the click function and print "x" on the clicked button but it doesnt change the text of the button and i dont have an idea why.
please help me.
here is the code:
<script>
var bigTable = "<table align='center' value='bigBoard' border='0' cellpadding='1' cellspacing='1'\><tbody>";
for (var k = 0; k < 9; k++)
{
if (k % 3 === 0)
{
bigTable += "<tr>";
}
bigTable += "<td>";
var mytable = "<table value='smallBoard'+k border='1' cellpadding='1' cellspacing='1'><tbody><tr>";
for (var j = 0; j < 3; j++)
{
for (var i = 0; i < 3; i++)
{
var position = +k + "," + j + "," + i;
mytable += "<td><button class='a' id=" + position + " type='submit' name=" + position + "></button</td>";
}
mytable += "</tr><tr>";
}
mytable += "</tr></tbody></table>";
bigTable += mytable + "</td>";
if (k % 3 === 2)
{
bigTable += "</tr>";
}
}
bigTable += "</tbody></table>";
document.write(bigTable);
</script>
<script>
$(document).ready(function() {
$(".a").click(function() {
var buttonPressed = $(this).attr("id");
jQuery.ajax({
data:buttonPressed,
url:"board",
success: function(responseText){
//alert("acac" + buttonPressed);
$("#" + buttonPressed).text(responseText);
}
});
});
});
</script>
This is what you are doing wrong, change this:
var position = +k + "," + j + "," + i;
to something like,
var position = +k + "-" + j + "-" + i;
Change the type of button from submit to button because you are calling Ajax request, not submitting the form.
I ran this jsfiddle and it works fine
Try it
http://jsfiddle.net/P8wrb/1/
Just declare a variable for your clicked button and then set the .text for it by reference
$(".a").click(function() {
var that = $(this);
....
that.text('X');
}