I am attempting to write either an array function or a dynamic array to Excel via a UDF written in Excel DNA (v.0.34). My result is always a single value instead of the array. What am I doing wrong?
[ExcelFunction(Name = "WriteTestArray")]
public static object[,] WriteTestArray()
{
try
{
return new object[2, 2] { { "one", "two" }, { "three", "four" } };
}
catch
{
return new object[,] { { ExcelError.ExcelErrorValue } };
}
}
For array functions to work with Excel (before the 'dynamic array' support that comes one day in a future version) you need to select the target range, then type in your formula and press Ctrl+Shift+Enter to commit it as an array formula. It will be indicated by curly brackets when displayed - e.g. {=MyFunc(...)}
Related
Suppose I have a recipe called Garlic parmesan butter. I need to return an object when the appropriate name has been found.
Now in a simple ad-hoc solution I can search in the following way:
class SearchRecipe {
late RecipeModel recipe;
RecipeModel returnRecipe(String? suggestion) {
for (int i = 0; i < Store.instance.getAllRecipes().length; i++) {
if (suggestion == Store.instance.getAllRecipes()[i].recipeName) {
return Store.instance.getAllRecipes()[i];
}
}
return recipe;
}
}
But I need a simple way where if the user types in Garlic butter I need to return the object associated with the Garlic Paremesan butter.
How can I do it?
Edit: I should've clarified that I'm working with a List of objects. So the Store.instance.getAllRecipes() basically returns a List<RecipeModel>.
Update 1: This is what I've written:
class SearchRecipe {
//late RecipeModel recipe;
RecipeModel returnRecipe(String? suggestion) {
List<RecipeModel> results = [];
suggestion!.split(' ').forEach((s) {
results.addAll(Store.instance
.getAllRecipes()
.where((element) => element.recipeName!.contains(s)));
});
results = results.toSet().toList();
for (int i = 0; i < results.length; i++) {
return results[i];
}
return results[0];
}
}
String search = 'Garlic butter';
List<String> list = [
'Garlic Paremesan butter',
'Butter',
'Garlic',
'Butter Garlic',
'Paremesan',
'Stackoverflow'
];
List<String> results = [];
search.split(' ').forEach((s) {
results.addAll(list.where((element) => element.contains(s)));
});
// Avoid repeated values
results = results.toSet().toList();
Split the user input at spaces. Then you have a list. You can check the list and depending on your preference implement a variety of behaviors.
You can match if all words in the list are found. You can return if at least one of the words is matched.
You could give preference to more contained words or you could check the order of words.
In either case you would also not check for equality but use a function like includes / contains to check whether the searched word is part of the name.
(Checking the order could be done by remembering which words you already identified and only searching after the words that were found. In your example you would find ‘garlic’ and after that you would just look through ‘paremesan Butter’ and try to find ‘butter’)
first split the input text
var string = "Hello world!";
string.split(" ");
// ['Hello', 'world!'];
then iterate over each word in above array and check whether the above
Store.instance.getAllRecipes()[i].recipeName contains above word.
if(str.equalsIgnoreCase(str))
{
//remaining code
}
try contains method.
.contains()
I have a class that contains a field called 'notes'. Its data type is array. In the mutation, I'm able to save data on the field. problem is it overwrites the value. I want to add value in the array. how do I do it? here's my mutation. I know I'm not doing the right thing, but can't find it in the documentation as well
mutation {
updateParcel(input: { id: "B6aESEwWcA", fields: { notes: ["wow"]}}) {
parcel {
objectId
notes {
... on Element {
value
}
}
}
}
}
I have a simple dictionary such as follows:
let testDict = [
"a": "apple",
"b": "banana",
"c": "cherry"
]
To render it in a view, using an answer from SwiftUI iterating through dictionary with ForEach, I use:
ForEach(testDict.sorted(by: >), id: \.key) { key, value in
Text(value)
}
and it works well enough. I would now like to filter the list with an if block as per SwiftUI: ForEach filters boolean .However, if I attempt to include an if block:
ForEach(testDict.sorted(by: >), id: \.key) { key, value in
if key != "b" {
Text(value)
}
}
XCode seems to hang (it just takes a very long time) and eventually gives up with these messages:
Command CompileSwift failed with a nonzero exit code
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
at the very end of the ContentView struct. This is not very helpful, and I can’t see why it takes XCode so long to give up.
How do I include an if block in a ForEach ?
I real life, the dictionary comes from a CoreData model, but the above sample gives the same problems.
I am developing on XCode 12.1, targeting MacOS 10.15
Update
I have since had success with the following:
ForEach(testDict.filter({$0.key != "b"}).sorted(by: >), id: \.key) { key, value in
Text(value)
}
You need view in ForEach content, so use Group, like
ForEach(testDict.sorted(by: >), id: \.key) { key, value in
Group {
if key != "b" {
Text(value)
}
}
}
I have json collection, which is contains nested elements as an array or key value pair and can be nested upto any length. I need to pass through an array, and find the items within it and convert them with their respective values.
For example, in the given below array, I have an Array
One function to just get the values
1. I will pass this array to find out these two cells within the JSON collection and convert them into value. These values can be anywhere in the tree. I need to pick up the cell and their parent node. and it should search them put their respective value in it so it will chanege from ["TRC030-A", "TRSEE050-A"] to [22, 12]
One function to just sum up the values
2. Please note there are collections, and in them, the cell is same. but if I pass the "NSEE050-A","NSEE060-A" in the example, it should pick them and put the value and sum them up
I need to do this javascript recursively.
Kind regards,
{
"MusicVersion": "1.0.0",
"validationVersion": "1.0.0",
"submissionStage": "editing / submitted for approval etc.",
"PiaonoData": {
"musicacademies": [
{
"name": "Music Name",
}]
},
"MainData": {
},
}
You could use a Map to map the keys in your search-array to their index in that same array. That map will allow to check quickly whether a property in the data matches any property and give its index. It would also work with indexOf, but a Map is faster.
For the rest is a recursive function: it can be exited as soon as all keys have been found:
function mapKeys(obj, keys) {
const map = new Map(keys.map((key, i) => [key, i]));
const result = [];
function recur(obj) {
for (prop in obj) {
if (Object(obj[prop]) === obj[prop]) {
recur(obj[prop]);
} else if (map.has(prop)) {
result[map.get(prop)] = obj[prop];
map.delete(prop);
if (!map.size) return;
}
}
}
recur(obj);
return result;
}
// Sample data
const data = {"MusicVersion": "1.0.0","validationVersion": "1.0.0","submissionStage": "editing / submitted for approval etc.","PiaonoData": {"musicacademies": [{"name": "Music Name","id": "Music ID / UPIN","data": "See Example Form Object","SCI040": "newly admitted member"}]},"MainData": {"mainBalance": {"CAATOT" : 0,"AFC020-A": 11,"TRC030-A": 22,"TRC040-A": 33,},"nonMainData": {"TRSEE050-A": 12,"staffEmoluments" : [{"NSEE050-A": 12,"NSEE050-B": 22,"NSEE050-C": 40,"NSEE050-D": 54},{"NGEE050-A": 36,"NGEE050-B": 41,"NGEE050-C": 9,"NGEE050-D": 0},{"NLEE050-A": 1,"NLEE050-B": 3,"NLEE050-C": 7,"NLEE050-D": 9}],"MuiscSpecialPayments": [{"LSRP010-A": 12,"LSRP010-B": 22,"LSRP010-C": 40,"LSRP010-D": 54},{"LDSP010-A": 36,"LDSP010-B": 41,"LDSP010-C": 9,"LDSP010-D": 0},{"LDSP010-A": 1,"LDSP010-B": 3,"LDSP010-C": 7,"LSSP010-D": 9}],"MusicConversions": [{"type": "simple/complex/conversion","TATI010-A": 1,"TATI010-B": 3,"TATI020-A": 7,"TATI030-B": 9}]}},"MusicData": {"AatOverview": { "TATI010-A": 1,"ABCD": 3,"DEF": 7,"KLM": 9},"acOverview": {"TATI010-A": 1,"ATAATI010-B": 3,"OPQ": 7,"ATAATI030-B": 9,},"musicacademies": [{"name": "Music Name","id": "MusicID","data": "See Example Form Object","MCI040": "Newly admitted"}]},"otherMusicData": { "tbc": null },"MusicCompletionStatuses": { "tbc": null },"MusicValidationExplanations": {"ABC1001": {"fieldValue": "600","userComment": "Extra spend"},"ABS1196": {"fieldValue": "30","userComment": "This is the reason "}},"lastUpdatedBy": "user namer","lastUpdatedDate": "2016-04-23T18:25:43.511Z"};
console.log(mapKeys(data, ["TRC030-A", "NSEE050-A"]));
I want to perform a query that will return all the documents that contain a given string in ANY of their fields. For example, say I have a "users" table and I'm looking for all the documents that contain "john", the returned result can be:
[{"first_name": "jhon", "last_name": "watson"}, {"first_name": "elton", "last_name": "john"}, {"first_name": "sherlock", "last_name": "holmes", "best_friend": "john watson"}]
How do I do that in rethinkdb? javascript answer will do, python implementation will be better.
Unfortunately this query is made harder by the fact that ReQL doesn't have a values function like python. However it does have a keys function so let's just use that to make a values function like so:
def values(doc):
return doc.keys().map(lambda x: doc[x])
Now that we have that finding a document that contains a string in one of its keys is pretty easy:
def has_str(doc, str):
return values(doc).map(match(str)).reduce(lambda x,y: x | y)
Finally we can put it all together:
r.table("users").filter(lambda doc: has_str(doc, str))
You could in theory do this all in one big query but I really like breaking up even moderately complicated queries. The really nice thing about this approach is that if it doesn't work each function has a pretty simple set of semantics so you can debug them individually.
It looks like you can just coerce the entire document into a string, and then search on that:
r.db('database').table('table).filter(function(doc) {
return doc.coerceTo('string').match('querystring');
});
This solution doesn't feel as slick as the other provided, but runs much faster for me and provides the same results so far.
For anyone who has found themselves here trying to figure this out in javascript, this roughly translates to:
function values(doc) {
return doc.keys().map(function(key) {
return doc(key);
});
}
function contains(doc, string) {
return values(doc).map(function(val) {
return r.branch(val.match(string), true, false);
}).reduce(function(left, right) {
return left.or(right);
});
}
var query = r.db('database').table('table').filter(function(doc) {
return contains(doc, "some string");
});
query.run().then(function(results) {
console.log(results);
});
Improvements welcome!