SpreadJS FromJson Chart load - spreadjs

I'm using SpreadJS v12 as a reporting tool. User will enter the page get the wanted data, create charts and save it for later use.
When user saves the report I get Json data (GC.Spread.Sheets.Workbook.toJSon) and save this Json to database and whenever someone tries to reach the same report, I get the Json from database and give it to the page (GC.Spread.Sheets.Workbook.fromJSon). Everything works fine except if there is a chart on page the data source for chart series (xValues and yValues) change. When I check Json format it looks like this: Sheet2!$B$2:$B$25 but in chart it's: Sheet2!$A$1:$A$24 . Am I doing something wrong?
By the way my serialize options: { ignoreFormula: false, ignoreStyle: false, rowHeadersAsFrozenColumns: true, columnHeadersAsFrozenRows: true, doNotRecalculateAfterLoad: false }
this.state.spread = new GC.Spread.Sheets.Workbook(document.getElementById("spreadSheetContent"), { sheetCount: 1 });
This is my save method:
var pageJson = this.state.spread.toJSON(this.serializationOption);
let self = this;
let model = {
Id: "",
Name: reportName,
Query: query,
PageJson: JSON.stringify(pageJson)
}
this.post( { model }, "Query/SaveReportTemplate")
.done(function(reply){
self.createSpreadSheet(reply);
}).fail(function(reply){
self.PopUp(reply, 4 );
});
And this is my load method:
var jsonOptions = {
ignoreFormula: false,
ignoreStyle: false,
frozenColumnsAsRowHeaders: true,
frozenRowsAsColumnHeaders: true,
doNotRecalculateAfterLoad: false
}
this.state.spread.fromJSON(JSON.parse(template.PageJson),jsonOptions);
this.state.spread.repaint();

Well after a long day, I think I've found what's causing the problem and started working around that.
Let's say we have two sheets. Sheet1's index is 0 and Sheet2's index is 1.
Because of the json serialization options like frozenColumnsAsRowHeaders and frozenRowsAsColumnHeaders until Sheet2 is painted row numbers and column number are different in the json.
If there is a formula or a chart in Sheet1 that's referencing Sheet2, their references will point to a different cell from what you set first. So always referencing the sheets that will be painted before is the way to solve this problem.

Related

I want to get data from JavaScript with Django

There is price filtering written in JavaScript in the template. I want to take the price range given in this filter with dajngo and write a filtering function. I couldn't because I don't know JavaScript. How will I do?
So, i want to write a django function that takes the given start and end values and sorts the products accordingly.
main.js
// PRICE SLIDER
var slider = document.getElementById('price-slider');
if (slider) {
noUiSlider.create(slider, {
start: [1, 100],
connect: true,
tooltips: [true, true],
format: {
to: function(value) {
return value.toFixed(2) + '₼';
},
from: function(value) {
return value
}
},
range: {
'min': 1,
'max': 100
}
});
}
I'm not familiar with noUiSlider but you would need to get the from and to values into Django - you can do that either by submitting a form when clicking FILTER or by sending an AJAX request. I presume you would just submit the form in a standard page submission as you aren't familiar with JS (and therefore AJAX).
def your_view(request)
filter_from = request.POST.get('slider_from')
filter_to = request.POST.get('slider_to')
YourModel.objects.filter(value__gte=filter_from, value__lte=filter_to)
...
You will need to replace slider_from and slider_to with the key values that are sent by the slider input in request.POST - this will be the name of the inputs themselves. You can wrap request.POST in a print statement to easily see what these are. It's just a matter of getting the values and passing them into the filter() function of your model.

Invalid filter value on Power BI chart

I'm using a Power BI embedded chart, with filters passed in from Javascript. Because of the way the filters are defined in a database, there is a possibility that a filter could be applied with the wrong type (a string supplied for a filter that's expecting a integer, for example). If this happens then the filter is invalid, and not applied, meaning a user can potentially see information that they shouldn't.
The code for setting up the chart is:
var powerBiDashboard = powerbi.load(embedContainer, config);
powerBiDashboard.on('error', function () {
console.log('error');
});
powerBiDashboard.on('loaded', function (event) {
console.log('Loaded');
powerBiDashboard.setFilters(
[{ $schema: "http://powerbi.com/product/schema#basic",
filterType: 1,
target:
{ table: "DummyData", column: "Count" },
operator: "In",
values: ["150"],
displaySettings: { isLockedInViewMode: true }
}])
.catch(function (errors) {
console.log(errors);
});
powerBiDashboard.render();
});
(note the incorrect value of "150" for the integer filter)
This produces a chart with this in the filter:
...instead of:
If this happens, I want to not display the chart, and instead navigate away to an error page.
So... how can I tell if the filter is invalid? The error event doesn't get fired on the powerBiDashboard object, and the catch on the setFilters method call doesn't get executed either. And I can't find any properties on the powerBiDashboard object that would help.
Thanks.
First of all, Power BI JS filters are not recommended to be used with sensitive data. Instead, apply RLS on report.
As of today, the Power BI JS does not have this feature where invalid filter values throw exceptions. The Power BI Embedded team is aware of this scenario and they will add support for it in future releases.
To circumvent the scenario today, you can check the type of value before applying and accordingly show warnings/errors to users.
Visit docs for more details about applying filters using Power BI JavaScript.

How to make Date Editable while using Glass mapper

Today i am facing one issue which has following requirement.
Date should be Editable.
Date should be in particular format.
My Code is like below which is not working.
foreach(var item in Model)
{
<div>#Editable(item, x => x.Start_Date.ToString("MMMM dd,yyyy"))</div>
}
I have tried following approach but throwing "DateParameters" namespace error.
#Editable(item, x=> x.Start_Date, new DateParameters { Format = "MMMM dd,yyyy"})
Also i have learner following thing but how can i achieve this ?
To make a field editable takes two parameters, this has been used to make the Date field editable. The first parameter instructs Glass.Mapper which field to make editable, the second parameter then specifies what the output should be when the page is not in page editing mode. This allows you to control the output of the field when in the two different modes.
Can anybody help me ?
For Experience editor mode, this works for me in razor view:
#Editable(model => model.SomeDateField, new { Format = "dd-MM-yyyy" })
Sitecore 8.2 though, with Glass 4.4.
What you want to do is to provide the default format but keep things the same for the main glass stuff. Like so:
foreach(var item in Model)
{
<div>#Editable(item, x => x.Start_Date, x=>x.Start_Date.ToString("MMMM dd,yyyy"))</div>
}
This will make the date a normal date when editing, but allow you to format it for the final page.
Usually in this case i use different code for "Normal View" and "Experience Editor", so for normal view you need only to display the date with format without making it editable, and on experience editor you need only to edit the date field the author will not care about the date format with experience editor, so your code will be like this :
foreach(var item in Model)
{
{
#if (Sitecore.Context.PageMode.IsExperienceEditorEditing)
{
<div>#Editable(item, x => x.Start_Date)</div>
}
else
{
<div>#item.Start_Date.ToString("MMMM dd,yyyy")</div>
}
}
}
I have tried that as well but it is throwing an error like below
**Value cannot be null. Parameter name: objectToSwitchTo
at Sitecore.Diagnostics.Assert.ArgumentNotNull(Object argument, String argumentName)
at Sitecore.Common.Switcher2.Enter(TValue objectToSwitchTo)
at Glass.Mapper.Sc.GlassHtml.MakeEditable[T](Expression1 field, Expression1 standardOutput, T model, Object parameters, Context context, Database database, TextWriter writer)**
Any help on this ?

CouchDB filter timestamps in a reduce function - some sort of Date.now?

Can a Date.now type function be used in either map or reduce functions? Can it be used anywhere at all?
More specifically, the view must not cache the Date.now value.
Here is what I tested that only worked for the first run after a change to any view function:
function (doc){
var n = new Date();
if(doc.TimeStamp > n.getTime() - 30000){
emit(doc._id, doc);
}
}
The view rows will be refreshed only when the particular doc gets updated. But you can request the view for that result: emit the doc.TimeStamp as key and request the view with ?startkey=timestamp where timestamp is the value of now.getTime() - 30000.
Yes. var now = new Date() should work.
The condition must result in false. You can test it with the view:
function (doc) {
var now = new Date()
var timestamp = now.getTime()
emit(timestamp,null)
}
It will respond something like
{
"total_rows":1,
"offset":0,
"rows" :[{
"id":"ecd99521eeda9a79320dd8a6954ecc2c",
"key":1429904419591, // timestamp as key
"value":null
}]
}
Make sure that doc.TimeStamp is a number (maybe you have to execute parseInt(doc.TimeStamp)) and greater then timestamp - 30000
Two words about your line of code emit(doc._id, doc);:
To emit doc._id as key means maybe you doesn't need the view. Simply request the doc by GET /databasename/:id. Also to include doc._id in multipart keys or the value of the view row is mostly not necessary because its included in every row automatically as additional property. One valid reason would be when you want to sort the view over the doc ids.
To emit the doc as value is not recommended for performance reasons of the view. Simply add ?include_docs=true when you request the view and every row will have an additional property doc with whole doc in it.

SimpleGeo and Google Maps

I'm trying to do something I thought would be simple. Query SimpleGeo, take the json and show the points on a google map.
The logic is as follows:
User views a page about a location stored in the Django DB with a simple google map. That map has one point showing the location.
User clicks show hotels
jQuery retrives json from SimpleGeo for a given lat/long
jQuery updates google map with points for hotes around that location.
Problems I'm having;
The json comes back with several entires. I've tried several looping functions but couldn't get anything but null. So then I just say json_ob[0] and I get one listing, but I can't get the cords from the json and show them on the map. Gmaps keeps compaining about the cords are not vaild. I've used alert() to check them but they seem to be the same format as the initial ones for the map.
I've also tried processing the json in my view and sending just the lat and long to the template but that doesn't seem to work either.
A link to a example or tutorial would be great.
I have here some
var obj = jQuery.parseJSON('{{ near_geo|safe }}');
cords = obj.geometry.coordinates
cords_array = cords.toString().split(',')
lng = cords_array[0];
lat = cords_array[1];
var marker = new google.maps.Marker({
map: map,
position: [lat , lng]
});
That produces:
Invalid value for property : 32.929272,-83.741676
If I could just get one to show up then I can loop and get the rest.
try to replace
var marker = new google.maps.Marker({
map: map,
position: [lat , lng]
});
with
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(lat, lng);
});