JQGrid: Export Grid to PDF - jqgrid

Is there any way of exporting JQGrid data to Excel/PDF. I am using SQL server 2008 R2 as database and WCF service for HTTP Request/response. Client is written using JavaScript and AJAX calls are made to interact with SQL database through WCF service.
Will 'excelExport' function of jqgrid work?
Here is the code to collect Grid Data and store:
enter code here
function ExportExcel() {
var mya=new Array();
mya = $("#PrjBudgetGrid").getDataIDs(); // Get All IDs
var data = $("#PrjBudgetGrid").getRowData(mya[0]); // Get First row to get the labels
var colNames=new Array();
var ii=0;
for (var i in data) {
colNames[ii++] = i;
} // capture col names
var html = "";
for (i = 0; i < mya.length; i++) {
data = $("#PrjBudgetGrid").getRowData(mya[i]); // get each row
for (j = 0; j < colNames.length; j++) {
html = html + data[colNames[j]] + "\t"; // output each column as tab delimited
}
html = html + "\n"; // output each row with end of line
}
html=html+"\n"; // end of line at the end
}

You can use the code from the answer or even better from another more recent answer. The part of the code which export data to Excel you can easy change to WCF code. See here an example how to use Stream as the output of WCF method.

Related

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.

highchart add new series and load data?

i am using javascript to add a series to a highchart. And i like to load the data to the series by an ajax call.
Here is my code:
function loadHighchartSeries(){
for (var i = 0; i < checkedGrpAdr3.length; i++) {
series_name = checkedGrpAdr3[i];
found = false;
for (var j = 0; j < chart.series.length; j++){
console.log(chart.series[j].name);
if (chart.series[j].name==series_name){
found = true;
}
}
if (!found){
datavar = ajax .... ????
chart.addSeries({
name: series_name,
data: datavar
});
}
}
}
The checkedGrpAdr3 is an array that contains the names of the series. First i check if the series name exists in the highchart graph. If it not exists it should load the data by using an ajax call and add a new series to the chart.
But how can i load the data by ajax and put it into the variable "datavar"?
Thanks

How to retrieve column names from a excel sheet?

Using EPPlus I'm writing data to multiple sheets. If a sheet is not created I'm adding a sheet else I'm retrieving the used rows and adding data from that row and saving it
FileInfo newFile = new FileInfo("Excel.xlsx");
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
var ws = xlPackage.Workbook.Worksheets.FirstOrDefault(x => x.Name == language.Culture);
if (ws == null)
{
worksheet = xlPackage.Workbook.Worksheets.Add(language.Culture);
//writing data
}
else
{
worksheet = xlPackage.Workbook.Worksheets[language.Culture];
colCount = worksheet.Dimension.End.Column;
rowCount = worksheet.Dimension.End.Row;
//write data
}
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
xlPackage.Save();
And it is working great.
Now I want to retrieve the column names of each sheet in the excel using LinqToExcel and this is my code
string sheetName = language.Culture;
var excelFile = new ExcelQueryFactory(excelPath);
IQueryable<Row> excelSheetValues = from workingSheet in excelFile.Worksheet(sheetName) select workingSheet;
string[] headerRow = excelFile.GetColumnNames(sheetName).ToArray();
At header row it is throwing me an exception
An OleDbException exception was caught
External table is not in the expected format.
But I don't want to use Oledb and want to work with Linq To Excel.
Note: When I'm working with single sheet rather than multiple sheets
it is working fine and retrieving all columns. Where am I going wrong.
(Based on OP's Comments)
The AutoFitColumn function has always been a little touchy. The important thing to remember is to call it AFTER you load the cell data.
But if you want a use a minimum width (when columns are very narrow and you want to use a minimum) I find EPP to be unreliable. It seems to always use DefualtColWidth of the worksheet even if you pass in a minimumWidth to one of the function overloads.
Here is how I get around it:
[TestMethod]
public void Autofit_Column_Range_Test()
{
//http://stackoverflow.com/questions/31165959/how-to-retrieve-column-names-from-a-excel-sheet
//Throw in some data
var datatable = new DataTable("tblData");
datatable.Columns.Add(new DataColumn("Nar", typeof(int))); //This would not be autofitted without the workaround since the default width of a new ws, usually 8.43
datatable.Columns.Add(new DataColumn("Wide Column", typeof(int)));
datatable.Columns.Add(new DataColumn("Really Wide Column", typeof(int)));
for (var i = 0; i < 20; i++)
{
var row = datatable.NewRow();
row[0] = i;
row[1] = i * 10;
row[2] = i * 100;
datatable.Rows.Add(row);
}
var existingFile2 = new FileInfo(#"c:\temp\temp.xlsx");
if (existingFile2.Exists)
existingFile2.Delete();
using (var package = new ExcelPackage(existingFile2))
{
//Add the data
var ws = package.Workbook.Worksheets.Add("Sheet1");
ws.Cells.LoadFromDataTable(datatable, true);
//Keep track of the original default of 8.43 (excel default unless the user has changed it in their local Excel install)
var orginaldefault = ws.DefaultColWidth;
ws.DefaultColWidth = 15;
//Even if you pass in a miniumWidth as the first parameter like '.AutoFitColumns(15)' EPPlus usually ignores it and goes with DefaultColWidth
ws.Cells[ws.Dimension.Address].AutoFitColumns();
//Set it back to what it was so it respects the user's local setting
ws.DefaultColWidth = orginaldefault;
package.Save();
}
}

How to get some output from an Oracle Data Modeler Rhino script?

I'm working on Rhino scripts to run into Oracle Data Modeler tool and sometimes I need to get a simple output from these scripts, like a list of objects (entities, tables, etc) and some data about them.
How can I do that?
One technique that can be used is to create a Note object and use this function to update the note contents with some arbitrary text.
var print = (function() {
var notes = model.getNoteSet().toArray();
var note = null;
if (notes.length > 0) {
note = notes[0];
note.comment = "";
}
return function() {
if (note != null) {
var s = String(note.comment);
for (var i = 0; i < arguments.length; i++) {
s += arguments[i];
}
note.comment = s;
}
}
})();
You can use it this way:
print("This ", "is ", "a test", "\n");
I know I can use java API to open a text file or something like that, but update a note content seems simpler to me.

Add an image/button in Google Script

I just added a script to a Form/Google Spreadsheet. It grabs the Response URL from the Form and pushes it into a column in the response spreadsheet. I would like to have the URL linked to a button(In html, I would of course anchor my image with the Edit Response URL, but now I am a little confuse, since I am not a super experienced script editor). How would that be possible to integrate it to my script?:
function assignEditUrls() {
var form = FormApp.openById('1-Sxpvd9jktE-SVXV0_dfp018xwcIoa3aXMA_fdff9W8');
//enter form ID here
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
//Change the sheet name as appropriate
var data = sheet.getDataRange().getValues();
var urlCol = 5; // column number where URL's should be populated; A = 1, B = 2 etc
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]]);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}
Its not possible to programatically add buttons or images to spreadsheets.
what you can do is add the url in those cells as a fomula =hyperlink("url",yoururl) so it looks prettier.

Resources