Export HTML Table to EXCEL File using php - export-to-excel

I would like to ask for code which output a html table into excel given that the html table is in result.php
Thanks!

If your data is a pure table (i.e. no graph), you should consider using the csv (http://en.wikipedia.org/wiki/Comma-separated_values) format instead of excel. I don't knwow your skills in programmation, but this should not be hard to do.

$test="<table border=1><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header('Pragma: public');
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=myfile.xls");
echo $test;

function exporttoexcel(){
var i;
var j;
var mycell;
var tableID = "crtfdetails";
var objXL = new ActiveXObject("Excel.Application");
var objWB = objXL.Workbooks.Add();
var objWS = objWB.ActiveSheet;
for (i=0; i < document.getElementById(tableID).rows.length; i++)
{
for (j=0; j < document.getElementById(tableID).rows(i).cells.length; j++)
{
mycell = document.getElementById(tableID).rows(i).cells(j)
objWS.Cells(i+1,j+1).Value = mycell.innerText;
}
}
objWS.Range("A1", "Z1").EntireColumn.AutoFit();
objXL.Visible = true;
}

Related

How can I use a For loop to loop tabs on sheet index numbers in Google Apps Script?

How can I use 'i' to loop through the sheet index numbers?
I tried this:
function myFunction() {
for (var i = 2; i < 14; i++);{
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getSheets()[i].getRange('J10').setFormula('=iferror(VLOOKUP($B9,\'STUDENT SCORES\'!AL46:AM49,2,FALSE)," ")');
}
}
But it does not work.
Thanks for the help!
Use this sample script:-
function myFunction() {
var spreadsheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i = 2; i < spreadsheets.length; i++);{
spreadsheets[i].getRange('J10')
.setFormula('=iferror(VLOOKUP($B9,\'STUDENT SCORES\'!AL46:AM49,2,FALSE)," ")');
}
}

The URL that should display a Google Sheet data in JSON format shows internal error

I want to convert the data of a Google Sheet into JSON format so that I can use it on my website. However, I get a 500 error whenever the website tries to fetch the JSON file.
I have already tried different methods to convert my sheet into JSON that are available on the internet
$url = 'https://spreadsheets.google.com/feeds/list/1dpmzzKNR8hRILX6qMD0KTruxxXYT3UAXR0EcX0zS0dE/1/public/full?alt=json';
$file= file_get_contents($url);
$json = json_decode($file);
$rows = $json->{'feed'}->{'entry'};
return $rows;
I had the same problem; I was able to work around the problem by parsing the html from the pubhtml page directly to output a similar JSON format:
Instead of using the Google Sheet ID, use the link for "Publish to Webpage".
There are some rows that I skip since some are for frozen rows, but you should be able to modify the code to your needs.
function importGoogleSheets(publishedUrl, sheetId, onSuccess, onError) {
var headers = [];
var rows;
$.ajax({
url: publishedUrl+'?gid='+sheetId+'&single=true',
success: function(data) {
data = $.parseHTML(data)[5];
htmldata = data;
rows = data.getElementsByTagName('tr');
for (var i = 0; i < rows[1].cells.length; i++) {
headers[i] = 'gsx$' + rows[1].cells[i].textContent.toLowerCase().replace(/[^0-9a-z]*/g, '');
}
for (var i = 3; i < rows.length; i++) {
temp = {};
for (var h = 1; h < headers.length; h++) {
temp[headers[h]] = {'$t': rows[i].cells[h].textContent};
}
all_data[i - 3] = temp;
}
onSuccess(all_data);
},
error: function(data) {
onError(data);
}
});
}
One note though is that it includes any empty rows unlike the feed, so you may want to filter the ouptut based on some column.

Kendogrid population

I'm new to all this kendo stuff i need help in populating kendogrid from a csv file.
The csv data is stored in an array of strings returned by a service.
Data looks like :
0: "Module,LogLevel,LogType,LoggedTime,LogMessage"
1: "00D02D5A4B66 ,CommServer ,Level3 ,Information ,03/16/2015 00:32:57:5716 ,[ISOMMessageHandler::Initialize]-[EventCount:20,ObjectRetryCount:6]"
2: "00D02D5A4B66 ,CommServer ,Level1 ,Information ,03/16/2015 00:32:57:5716 ,ISOMProtocolHandler::HandleConnectGeneric] - Before UpdatePanelTouched - CommServerID : 1, ConnectionMode : 2"
3: "00D02D5A4B66 ,CommServer ,Level4 ,Information ,03/16/2015 00:32:57:5716 ,[PanelDataConfigurationHandler : UpdatePanelConnectionStatus] : CommServerID 1, CommMode : 2"
i need to display 0th indexed data as title of the columns
and rest in cells of the column.
My advice is to make a wrapper method yourself and get it into JSON.
needed wrapper as told by Thomas.
here is my wrapper function
function csvJSON(lines) {
var result = [];
var headers = lines[0].split(",");
headers.unshift("MAC");
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
return result;
}

How do I fix charset problems in .gs script?

I have a problem with charsets.
I parsed a csv file in google-app-engine and I'm posting to an uiapp table.
But I checked special characters like áéíóú and those are not well displayed (?square symbol).
When I was setting up my code I played writing the string imported to a google docs document and it worked the same.
some advice please?
I search for:
a global charset definition to the code. or
string var transformation that makes the chars appear like I want to. (avoiding html &number definitions.
Is this related to the blob object?
The thing is important i come from spain and we need such characters.
app that get's a csv ';' delimited file and shows it's content
I post all my code, it's barely as the tutorial that is given.
function arreglaUrl(cadena){
var texto = cadena[cadena.length - 2]
if (texto == ''){
cadena[cadena.length - 2] = 'Sin enlace';
}
else{
cadena[cadena.length - 2] = '<center>Link.</center>' ;
};
}
function parsedCSV(){
var listaArchivos = DocsList.getFolderById('XXXXX').getFiles()
for (var i = 0; i < listaArchivos.length; i++) {
if (listaArchivos[i].getName() == 'baul.csv'){
var origen = listaArchivos[i];
};
}
var texto = origen.getContentAsString();
var arra = Utilities.parseCsv(texto,";");
return(arra);
}
function doGet() {
var datos = parsedCSV()
var baul = Charts.newDataTable()
for (i = 0; i < datos[0].length; i++){
baul.addColumn(Charts.ColumnType.STRING, datos[0][i])
}
for (i = 1; i < datos.length; i++){
arreglaUrl(datos[i]) // this only makes some html i need to post some links
baul.addRow(datos[i])
}
baul.build();
var sectorFilter = Charts.newCategoryFilter()
.setFilterColumnLabel("sector")
.build();
var tipoFilter = Charts.newCategoryFilter()
.setFilterColumnLabel("tipo")
.build();
var searchFilter = Charts.newStringFilter()
.setFilterColumnLabel("Titulo")
.build();
var searchDesc = Charts.newStringFilter()
.setFilterColumnLabel("descripcion")
.build();
var tableChart = Charts.newTableChart().setOption('allowHtml', true).setDimensions(0,0)
.build();
var dashboard = Charts.newDashboardPanel()
.setDataTable(baul)
.bind([sectorFilter, tipoFilter, searchFilter, searchDesc], [tableChart])
.build();
var uiApp = UiApp.createApplication().setTitle('Baul de Recursos');
var anchoTotal = '100%';
dashboard.add(uiApp.createVerticalPanel()
.add(uiApp.createHorizontalPanel()
.add(sectorFilter)
.add(tipoFilter)
.setSpacing(15)
)
.add(uiApp.createHorizontalPanel()
.add(searchFilter)
.add(searchDesc)
.setSpacing(15)
)
.add(uiApp.createHorizontalPanel()
.add(tableChart).setBorderWidth(1).setHorizontalAlignment(UiApp.HorizontalAlignment.CENTER).setWidth(anchoTotal)
)
);
uiApp.add(dashboard);
return uiApp;
}
I found it, we need to get the content of the file first with a Blob object.
This function is the one I use to parse some csv info into an array:
function parsedCSV(){
//searching the file. This gets only one file in var origen
var listaArchivos = DocsList.getFolderById('XXXXXXX').getFiles()
for (var i = 0; i < listaArchivos.length; i++) {
if (listaArchivos[i].getName() == 'baul.csv'){
var origen = listaArchivos[i];
};
}
// HERE IS THE GOOD DEFINITION OF CHAR:
var texto2= origen.getBlob().getDataAsString('ISO-8859-1');
// I put all the corrected text in an array
var arra = Utilities.parseCsv(texto2,";");
return(arra);
}
This is the solved thing: https://script.google.com/macros/s/AKfycbyHa-bLWBHBr3qifbvzxecqGgGUYX8mhyo-TKoyfGvy/exec
The trick:
var textVariableName = fileObjectVariableName.getBlob().getDataAsString('ISO-8859-1');

Abcpdf copyable/selectable text

i'm using websupergoos abcpdf to convert html pages to pdf via addimageurl.
Works great, but the resulting pdf does not allow the user to select text and copy. All is one 'image'.
Is it possible to do this? Which are the settings to use?
This is my current code. The commented "flatten" does not seem to do anything relevant. The HttpStream simply forewards the pdf to users as a doc.
var doc = new Doc();
doc.HtmlOptions.UseScript = true;
doc.Units = "mm";
doc.MediaBox.String = "0 0 210 297";
doc.Rect.String = doc.MediaBox.String;
doc.Rect.Inset(10.0, 10.0);
doc.SetInfo(0, "License", abcpdfkey);
doc.HtmlOptions.UseScript = true;
doc.HtmlOptions.AddMovies = true;
doc.HtmlOptions.RetryCount = 0;
doc.HtmlOptions.ContentCount = 1;
doc.Page = doc.AddPage();
for (int i = doc.AddImageUrl(url); doc.Chainable(i); i = doc.AddImageToChain(i))
{
doc.Page = doc.AddPage();
}
int pageCount = doc.PageCount;
for (int j = 1; j <= pageCount; j++)
{
doc.PageNumber = j;
// doc.Flatten();
}
this.HttpStream(doc.GetData(), filename);
Before sending the PDF to the HTTP stream, you can set the encryption properties
The CanCopy Property sets if the user can copy text from the PDF
To set it add the following code:
doc.Encryption.CanCopy = true;
You may need to set doc.Encryption.CanExtract as well

Resources