I have a grid for terminals, What I want to do now is to display the selected terminals into another grid. I am able to get the selected terminals with this code:
var sel = scope.getTerminalGrid().getSelectionModel().getSelection();
var user = scope.getProfinfo().getForm().getValues();
for(var i=0; i < sel.length; i++)
{
var terminals = sel[i].data;
}
You can simply get the data (in your case above its the 'sel' variable) and then load it in the second grid store.
Something like that:
var sel = scope.getTerminalGrid().getSelectionModel().getSelection();
var selRecords = sel.getSelection();
var secondGrid = scope.getMySecondGrid();
var secondStore = secondGrid.getStore();
secondGrid.removeAll(); //Clear the data
secondGrid.add(selRecords);
Of course, I separated all in a lot of variables, you can abstract some of them.
you can do it multiple ways, one is with a drag and drop grid group, another is grabbing the grid of second store like so .
var secondGrid = scope.getMySecondGrid()
secondGrid.getStore()
the store is the powerful underlying data structure of the grid. You can then load any data you want in there.
Related
I'm working on a form where I need to pull the contents of a spreadsheet column like 50 times, to try to input multiple items from a list. I see that I can do this by defining a few variables and redoing a small piece of Script again and again. I want to see if anyone can help me overcome this lengthy script to make it smaller with fewer iterations. Thanks.
function updateForm(){
// call the form and connect to the drop-down items
var Form_SQ = FormApp.openById("FORM ID");
var SQ_IT01_List = Form_SQ.getItemById("ITEM 01").asListItem();
var SQ_IT02_List = Form_SQ.getItemById("ITEM 02").asListItem();
//Similarly defining upto 50 dropdown lists.
var SS01 = SpreadsheetApp.getActive();
var SQ_IT01_Names = SS01.getSheetByName("Sheet2");
var SQ_IT02_Names = SS01.getSheetByName("Sheet2");
//Similarly defining upto 50 names lists.
// Item_01 Part Number Dropdown
var SQ_IT01_Values = SQ_IT01_Names.getRange(2, 1, SQ_IT01_Names.getMaxRows() - 1).getValues();
var SQ_IT01_Items = [];
for(var i = 0; i < SQ_IT01_Values.length; i++)
if(SQ_IT01_Values[i][0] != "")
SQ_IT01_Items[i] = SQ_IT01_Values[i][0];
SQ_IT01_List.setChoiceValues(SQ_IT01_Items);
// Item_02 Part Number Dropdown
var SQ_IT02_Values = SQ_IT01_Names.getRange(2, 1, SQ_IT02_Names.getMaxRows() - 1).getValues();
var SQ_IT02_Items = [];
for(var i = 0; i < SQ_IT02_Values.length; i++)
if(SQ_IT02_Values[i][0] != "")
SQ_IT02_Items[i] = SQ_IT02_Values[i][0];
SQ_IT02_List.setChoiceValues(SQ_IT02_Items);
//Similarly defining upto 50 lookup lists.
}
Problem
Reusing code and making use of loops. Scripting is all about efficiency (see DRY principle): make as little assignments and same-functionality coding as possible - use loops, move reusable code snippets to functions that can be called on demand, etc.
Solution
This sample makes several assumptions:
SQ_IT01_Names is different for each item (in your sample it always is Sheet2 - if this is the case, you don't have to reassign it 50 times, one variable assignment will do just fine).
You intended to do something when a value is an empty string (the sample just filters them out). As you use the [index] notation, those values in the resulting Array will be undefined (and that's not something one would want in an Array of choice values).
All items are choice items (if you need id filtering, the sample is easily expanded).
function updateForm() {
var form = FormApp.openById("FORM ID");
//access every item;
var items = form.getItems();
var ss = SpreadsheetApp.getActive();
//loop over items;
items.forEach(function(item,i){
var namesSheet = ss.getSheetByName('Sheet'+i); //assuming this is diff each time;
var namesRange = namesSheet.getRange(2,1,namesSheet.getLastRow());
var namesValues = namesRange.getValues();
//map values to first column;
namesValues = namesValues.map(function(value){
return value[0];
});
//filter out undefined (undefined and false functional equivalence);
namesValues = namesValues.filter(function(value){
return value;
});
item.asListItem().setChoiceValues(namesValues);
});
}
Notes
Please, use closures {} with loops and if statements, this way you'll be able to keep track of which statements are enclosed in it and save yourself debugging time when looping / conditioning multiple statements.
Since you only need rows that have data in them, use the getLastRow() method instead of the getMaxRows()-1 calc you have to perform in your script.
Reference
forEach() method reference;
filter() method reference;
map() method reference;
getLastRow() method reference;
I have a Google spreadsheet that I'm trying to remove the word "woo" within a range of cells
So far I've managed to loop through the results and log the results, however I haven't figured how to update that information in the spreadsheet itself.
Any guidance would be welcomed
Thank you
function myFunction () {
var ss = SpreadsheetApp.getActiveSheet().getRange('B:B')
var data = ss.getValues();
for (var i = 0; i < data.length; i++) {
var text = data[i].toString();
var finaltext = text.replace(/woo/g, "");
data[i] = finaltext;
Logger.log(data[i]);
}
}
Use setValues()
Notes:
Usually ss is used as a shorthand for spreadsheet, as it's used on the code for a range it's better to use range as a variable name.
setValues() returns a 2D array, so data[i] returns an array of row values rather than a cell value. To get/set cell values, use data[i][0] notation.
Considering the above replace
var ss = SpreadsheetApp.getActiveSheet().getRange('B:B')
by
var range = SpreadsheetApp.getActiveSheet().getRange('B:B')
then add the following line after the for block.
range.setValues(data);
Regarding text var declaration, replace
var text = data[i].toString();
to
var text = data[i][0].toString();
Using open ended references like B:B could lead to problems. To avoid them be sure to keep the sheet rows at minimum or better instead of using an open ended reference use something like B1:B10.
I am having a problem getting the default WinForms Chart control to work. I have a single chart with a single area. In this area, I want to display three (3) series whose labels are on a single legend.
Each of the value arrays shown in the code below contains six (6) values.
When I run the application the chart is only showing the background with the title and the name of the FIRST series I defined, the others seem to be ignored. Also, no grid and no data points or lines are displayed. The chart is basically blank.
this.chart.SuspendLayout();
this.chart.ChartAreas.Clear();
this.chart.Series.Clear();
ChartType chartType = ChartType.Column;
// prepare the area
const string AREA_NAME = "ChartAreaBP";
ChartArea bpChartArea = new ChartArea(AREA_NAME);
bpChartArea.AxisX.LabelStyle.Format = "dd/MMM\nyyyy";
bpChartArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.LightGray;
bpChartArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.LightGray;
bpChartArea.BackColor = System.Drawing.Color.LimeGreen;
bpChartArea.BackGradientStyle = GradientStyle.DiagonalRight;
bpChartArea.Position.Auto = true;
bpChartArea.InnerPlotPosition.Auto = true;
this.chart.ChartAreas.Add(bpChartArea);
// prepare the values. X is Date/time all other 3 are BYTE/INT
var xvals = from x in items select x.TimeStamp;
var yvalsSys = from y in items select y.Systolic;
var yvalsDia = from y in items select y.Diastolic;
var yvalsRhy = from y in items select y.Rhythm;
// The first series, other 2 omitted from HERE for simplicity
const string SYS_SERIES = "Systolic";
Series sysBPSeries = new Series(SYS_SERIES, 4);
sysBPSeries.ChartType = chartType;
sysBPSeries.ChartArea = AREA_NAME;
sysBPSeries.XValueType = ChartValueType.Auto;
sysBPSeries.YValueType = ChartValueType.Date;
sysBPSeries.XAxisType = AxisType.Primary;
sysBPSeries.YAxisType = AxisType.Primary;
sysBPSeries.Enabled = true;
this.chart.Series.Add(sysBPSeries);
this.chart.Series[SYS_SERIES].Points.DataBindXY(xvals, yvalsSys);
// here the other two series are defined.
But when I run the application only the legend of the FIRST series is shown even though the other two are defined in the code (I omitted them from this listing) just the same way as the first series.
And as I stated above, no grid nor values are shown. However, the chart shown in design mode does show all three labels on the first and only legend and all three lines.
Just to give you a little background on my question:
I am creating a form in Google App Script using the UI Services and I am storing specific calendar events in a dataArray. So the event object is stored in the array. I want to pass this array to the submit function but can't figure out how to go about this because :
I can't add it as a callback element (because it isn't a widget)
I can't store the event object in a widget (i.e. a listbox, etc) and then add that widget as a callback element.
Here is a brief sample of what I am trying to do:
var cal= CalendarApp.getDefaultCalendar();
var event= cal.getEvents(new Date("June 16, 2013 PST"),
new Date("July 22, 2013 PST"));
var specific = new Array;
for( var j=0; j<event.length;j++){
specific.push(event[j]);
//This stores the events in the specific variable
//I want to send this variable (w/ the data) to another function on submit
I would appreciate any suggestions you can lend me.
Thanks!
As a complement to the answer I gave in the comments : "you could also simply store the id and while you read the events again using the same start/end time you can check if an event correspond to the saved ID in a loop... if a match is found with the right ID you are sure it's the right event"
Here is a piece of code I use to modify/update/delete calendar events using their ID as reference. This code is used to delete specific events selected from the spreadsheet, the code to modify events is roughly the same, at least it uses the same ID checking.
...
var cal = CalendarApp.openByName(calName);
if (cal) {
var events = cal.getEvents(new Date(date_deb), new Date(date_fin),{max: 4000}); // stocke tt ds une variable array
var sel= sh.getRange(6,1,sh.getLastRow()-5, 10).getValues();// read data in the SS
for(e=0;e<events.length;++e){
var delFlag = false;
var ID = events[e].getId();
for(n=0;n<sel.length;++n){
if ((sel[n][8] == "x"||sel[n][8] == "X")&&sel[n][9]==ID){ // the ID here is stored in the spreadsheet in column J and I use a 'X' marker to select which event should be deleted
delFlag = true;
sh.getRange(n+6,9,1,2).setBackgroundColor('#ff5500');
SpreadsheetApp.flush();
Logger.log('FLAG '+ e);
break;
}
}
if(delFlag){
try{
var toDelete = events[e].deleteEvent();
++todel;
delFlag = false;
Logger.log('event deleted : '+sel[n][1]);
}catch(Err){Logger.log('Event from a serie already deleted from another occurence')}
}
}
}
var msg = todel + " événement(s) effacé(s) dans l'Agenda '"+calName+"'";
ss.toast("Effacement terminé", msg, 3);
...
I use ExtJs 3.3.1 because many extensions don't work under 4.xx
One of these extensions is LiveGrid.
I can't try but i suppose a simmilar thing happens with a 4.x buffered grid.
When i do a report of the lines visible in the grid only the buffered lines are returned, i reposition the current record but the loading of the rest of the records only happens after the reporting finishes. How can i get all the records ?
In an button handler i call toReport(grid).
toReport = function(grid){
var store = grid.getStore();
var view = grid.getView();
store.each(function(record) {
Ext.defer(function(){
index = readRow(store, record);
if (index % 10 == 0){
view.focusRow(index);
}
}, 500, this);
});
console.log(output)
}
readRow = function(store, record){
output = "";
for (var xlCol=1;xlCol<record.fields.length+1;xlCol++){
var veld = store.fields.itemAt(xlCol-1).name;
waarde = record.get(veld);
if (realTypeOf(waarde)==="date"){
output += waarde.format("d-m-Y");
}else{
output += record.get(veld);
}
}
console.log(store.indexOf(record)+ " " + output);
return store.indexOf(record);
}
The grid needs to manipulate its store filters, sorters, paging, etc., in order to obtain the records it want to display. The store itself only keeps in memory the subset of records that matches its filters, etc. That is the way stores are designed in Ext: they are intended to be bounded to one and only one view.
I think in your case, the simplest solution is to create another store with a similar configuration, and use its load method with params such that you get all the records.
If you're reticent to fire multiple requests for retrieving essentially the same data, have a look at Ext.data.Proxy. Unlike stores, proxies are not bound to a specific view or task and can be shared between multiple store instances. So in theory, you can create a proxy that requests all the records from the server at once, and then feeds a subset of them to multiple stores. For that you'll have to implement the doRequest method of the proxy (or most probably overrides the one of the proxy you're already using).
I did find a solution by using recursion.
Like this the view kan keep up with the enumeration.
Ext.toExcel = function(grid){
var store = grid.getStore();
var view = grid.getView();
readRow(store, view, 0);
}
readRow = function(store, view, index){
output = "";
record = store.getAt(index);
for (var xlCol=1;xlCol<record.fields.length+1;xlCol++){
var veld = store.fields.itemAt(xlCol-1).name;
waarde = record.get(veld);
if (realTypeOf(waarde)==="date"){
output += waarde.format("d-m-Y");
}else{
output += record.get(veld);
}
}
//console.log(index+ " " + output);
if (index % 50 == 0){view.focusRow(index);}
Ext.defer(function(){
if (index < store.totalLength){
readRow(store, view, index+1);
}
}, 100, this);
}