If I have an array, I can set the keys by doing the following:
var example:Array = new Array();
example[20] = "500,45";
example[324] = "432,23";
If I want to do something with Objects, how would I achieve this?
I tried the following:
var example:Object = [{x:500, y:45}, {x:432, y:23}]; // Works but keys are 0 and 1
var example:Object = [20: {x:500, y:45}, 324: {x:432, y:23}]; // Compile errors
var example:Object = [20]: {x:500, y:45}, [324]: {x:432, y:23}; // Compile errors
var example:Object = [20] {x:500, y:45}, [324] {x:432, y:23}; // Compile errors
Is there a good way to achieve this?
I understand I could do this:
var example:Object = {id20 : {x:500, y:45}, id324: {x:432, y:23} };
But it doesn't suit me.
The [] notation has the same meaning of doing a new Array() so when you are doing:
var example:Object = [{x:500, y:45}, {x:432, y:23}];
you are in fact creating an array with two elements who are object {x:500, y:45} and {x:432, y:23}.
If you want to create an object with key 20 and 324 use the {} notation who is the same a new Object()
So your example became =>
var example:Object = {20: {x:500, y:45}, 324: {x:432, y:23}};
You can do the same as your first example using an Object instead of an Array:
var example:Object = new Object();
example[20] = "500,45";
example[324] = "432,23";
Related
I want to create a master tab that takes data from other tabs. Once the data is in the master tab I want to be able to filter the information.
I've done this previously using the code that I am having issues with. I learned how to do this here: https://www.youtube.com/watch?v=rpITkG40rQs
Also the previous project I used this code for still works.
The code I am having trouble with is below. I'm getting the following error now:
Exception: The starting column of the range is too small.
getColumnValue # Copy 2 of Code.gs:59
getCombinedColumnValues # Copy 2 of Code.gs:39
(anonymous) # Copy 2 of Code.gs:16
combineData # Copy 2 of Code.gs:15
previously i got this error:
Syntax error: SyntaxError: Unexpected token ')' line: 14 file: Copy of Code.gs
line 14 is under the "labels" var and above the loop starting with lables.foreach...
function combineData() {
var masterSheet = "Master";
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(masterSheet);
var lc = ss.getLastColumn();
var lr = ss.getLastRow();
ss.getRange(2, 1, lr-1, lc).clearContent();
ss.getRange(1, 49, lr-1, lc).clearContent();
var labels = ss.getRange(1, 1, 1, lc).getValues()[0];
labels.forEach(function(label,i){
var colValues = getCombinedColumnValues(label);
ss.getRange(2, i+1, colValues.length, 1).setValues(colValues);
Logger.log(colValues);
});
}
/// note to self - the problem I was having with the document below was the 'if' statement. The tutorial only has one exclusion. I have several tables to include and exclude.
function getCombinedColumnValues(label) {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets()
var colValues = [];
for(k in sheets){
var sheetName = sheets[k].getSheetName();
// grabs the tabs I want.
if ((sheetName == "CBDIO Data")||(sheetName == "CCA Data")||(sheetName == "CHW Data")||(sheetName == "Mujeres en Accion Data")||(sheetName == "Padres Unidos Data")||(sheetName == "CBDIO - Form")){
var tempValue = getColumnValue(label, sheetName)
colValues = colValues.concat(tempValue);
}
};
return colValues;
}
function getColumnValue(label, sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var colIndex = getColumnIndex(label, sheetName);
var numRow = ss.getLastRow() - 1;
var colValues = ss.getRange(2, colIndex, numRow, 1).getValues();
return colValues;
}
function getColumnIndex(label, sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var lc = ss.getLastColumn();
var lookupRangeValues = ss.getRange(1, 1, 1, lc).getValues()[0];
var index = lookupRangeValues.indexOf(label) + 1;
return index;
}
'''
How can I create multiple objects in 1 Step in Dart? Something like:
Class Player{
var Health;
var Level; .... }
Somewhere else:
Player[] player = new Player[20];
How can I do that in Dart?
If you wanna create a lot "Players"... Try this:
var players = List.generate(20, (i) => Player(/* properties */));
Filling in from any source, you can use the "i" as the index.
var players = List.generate(20, (i) {
var sourceRef = source[i];
return Player(
health: sourceRef["health"]
);
});
You can create a list of Player using the following line:
List<Player> player = new List(20);
And then initialize each object of your player list :
for (var i in jsonResponse['participants']) {
player[x] = new Player() ; // add this to your code
var fill = player[x];
fill.health = i['health'];
x++;
}
You can find more information about the proper way of building and initializing list in the official Dart Documentation.
How can I combine/merge 2 or more maps in dart into 1 map?
for example I have something like:
var firstMap = {"1":"2"};
var secondMap = {"1":"2"};
var thirdMap = {"1":"2"};
I want:
var finalMap = {"1":"2", "1":"2", "1":"2"};
you can use addAll method of Map object
var firstMap = {"1":"2"};
var secondMap = {"2":"3"};
var thirdMap = {};
thirdMap.addAll(firstMap);
thirdMap.addAll(secondMap);
print(thirdMap);
Or
var thirdMap = {}..addAll(firstMap)..addAll(secondMap);
Update
Since dart sdk 2.3
You can use spread operator ...
final firstMap = {"1":"2"};
final secondMap = {"2":"3"};
final thirdMap = {
...firstMap,
...secondMap,
};
alternate syntax using Map.addAll, Iterable.reduce and cascading operator, for combining a lot of maps:
var combinedMap = mapList.reduce( (map1, map2) => map1..addAll(map2) );
live dartpad example
https://dartpad.dartlang.org/9cd116d07d2f45a9b890b4b1186dcc5e
Another option is using CombinedMapView from package:collection:
new CombinedMapView([firstMap, secondMap])
It doesn't create a merged map, but creates a Map that is a view of both.
I came up with a "single line" solution for an Iterable of Maps:
var finalMap = Map.fromEntries(mapList.expand((map) => map.entries));
var firstMap = {"1":"5"};
var secondMap = {"1":"6"};
var thirdMap = {"2":"7"};
var finalMap = {...firstMap, ...secondMap, ...thirdMap};
// finalMap: {"1":"6", "2":"7"};
Notice that key "1" with value "5" will be overwritten with "6".
While running this code, there is error while executing this line:
var invoiceSheet = newSSFile.getSheets()[0];'
"TypeError: Cannot find function getSheets in object Copy of Invoice Przykładowy. (line 69, file "Code")"
With this code I want:
Create a new spreadsheet and move it to proper folder [works]
Get a value from another spreadsheet and paste it in this new one [error]
Looked for answer for an hour without any result. Any idea what might cause this error?
function invoice() {
//Create copy SS + name
var ssTemp = SpreadsheetApp.openById("1Cr2W_4lNrHYRdXK-KDQ7UFJJ3Iagh-tGct8Ee5Y");
var newSS = ssTemp.copy("Copy of " + ssTemp.getName());
// Move to folder
var DestinyFolder = DriveApp.getFolderById("0B-y1OC8ChG2XRjRRbEJ");
var newSSFile = DriveApp.getFileById(newSS.getId());
DestinyFolder.addFile(newSSFile);
DriveApp.getRootFolder().removeFile(newSSFile);
// Modify details
// Invoice No
var klienciSS = SpreadsheetApp.openById("18B151VlJaVtDdQ9CcLrL3iwRAtWw2ZzZydproj");
var klienciSheet = klienciSS.getSheets()[0];
var klienciRange= klienciSheet.getRange('AB6');
var klienciValue = klienciRange.getValue();
var invoiceSheet = newSSFile.getSheets()[0];
var inboiceRange = invoiceSheet.getRange('F4');
newCellInvoice.setValue(klienciValue);
The problem is caused by the following line
var newSSFile = DriveApp.getFileById(newSS.getId());
It makes that newSSFile holds an instance of Class File instead of an instance of Class Spreadsheet
But the problematic line looks unnecessary. Replace the line that throws the error by
var invoiceSheet = newSS.getSheets()[0];
I'm trying to setup an output parameter using PetaPoco. I found someone using this sample online:
var ctx = new CustomDBDatabase();
var total = new SqlParameter("Total", System.Data.SqlDbType.Int);
total.Direction = System.Data.ParameterDirection.Output;
var results = ctx.Query<DBEntity>("exec GetDBEntities #StartIndex, #MaxIndex, #TotalCount = #Total out",
id, start, max, total);
int totalCount = (int)total.Value;
However, total.value returns null, even though when I run this statement directly against SQL Server, it returns me 3. Is this setup correctly with PetaPoco? Are output parameters supported?
Thanks.
This is supported. But your current syntax is wrong anyways.
var ctx = new CustomDBDatabase();
var total = new SqlParameter("TotalCount", System.Data.SqlDbType.Int);
total.Direction = System.Data.ParameterDirection.Output;
var results = ctx.Query<DBEntity>("exec GetDBEntities #StartIndex, #MaxIndex, #TotalCount OUTPUT", new { StartIndex = start, MaxIndex = max, TotalCount = total});
int totalCount = (int)total.Value;
Something like this should work though. Not quite sure of the sql syntax but this should get you on your way.