as docs say( https://github.com/cloudant/sync-android/blob/master/doc/query.md) im trying to do a query inside a list
look:
query.put("codigo", 4);
result = q.find(query);
for (DocumentRevision revision : result) {
Log.d("QueryTest", "Test1 :" + revision.getBody().toString());
}
return:
{ "codigo":4, "companies":[
{
"id":"b9f19d88-13c0-40e3-89de-63dc787afb4c",
"name":"Filial 0 1488949817178"
},
{
"id":"f17fb098-316e-4d33-a0f7-f5719bf9d62e",
"name":"Filial 1 1488949817178"
} ], "employees":[
{
"codigo":2891,
"id":"cc54fa37-0b64-4108-869a-1303c6176ce5",
"name":"Employee 0 79ed4"
},
{
"codigo":4642,
"id":"19b76bbc-82c7-4295-a385-82ac2d892458",
"name":"Employee 1 e1102"
} ], "id":"ef2d0ebf-50b9-4cd0-9aaf-5389bccaaa56", "nome":"Random 4" }
so its ok
but this query doesnt work:
query.put("employees.codigo", 2891);
first return:
QuerySqlTranslator: No single index contains all of
[{employees.codigo={$eq=2891}}]; add index for these fields to query
efficiently
after created the index:
Index i = q.createJsonIndex(Arrays.<FieldSort>asList(new FieldSort("employees.codigo")), null);
return
QueryExecutor: Projection fields array is empty, disabling project for
this query
so i created the arraylist of fields to filter
List<String> fields = Arrays.asList("codigo");
result = q.find(query, 0 , 0 , fields, null);
nothing returned
adding more one field to filter:
query.put("codigo",4)
query.put("employees.codigo", 2891);
returned: Complain about index
Created another index:
i = q.createJsonIndex(Arrays.<FieldSort>asList(new FieldSort("employees.codigo"), new FieldSort("codigo")), null);
returned: Nothing
whats is wrong?
How can i get document by child and how can i get ONLY the children?
thanks
I think this is unsupported, see the doc on unsupported features, specifically the highlighted entry:
Arrays
Dotted notation to index or query sub-documents in arrays.
Querying for exact array match, { field: [ 1, 3, 7 ] }.
Querying to match a specific array element using dotted notation, { field.0: 1 }.
Querying using $all.
Querying using $elemMatch.
Related
Found the answer -> dataclass copy with field ArrayList - change the ArrayList of the copied class changes the original
I have this wired bug. I have an array list of size 3. The third item is a duplicate of the second item in the list. I differentiate them based on their position on the list. The problem is that when I do a for loop to change the properties of the item at index 1 the item at index 2 reflects the same change.
What I've tried so far. ...
I have confirmed that the list is being changed at only one spot in the app.
Added more fields to differentiate the duplicate items.
Set if statements to make sure that I am only changing the specific item.
What I think the solution is.
My assumption is that since the items at index 1 & 2 have a coupled relationship since they are duplicates of each other. (even with differentiating factors) I don't know what this relationship is.
My code snippet.
private fun testing(selectedNormalModifier: ToppingModel) {
var modIndex = -1
selectedNormalModifier.parentPosition = selectedModifierPosition
selectedNormalModifier.localToppingName = modifierGroupModel!!.itemModifierGroups[selectedModifierPosition].itemLocalTitle.toString()
val itemToEdit = modifierGroupModel!!.itemModifierGroups[selectedModifierPosition]
for (i in itemToEdit.modifierGroups.modifiers.indices) {
val mod = itemToEdit.modifierGroups.modifiers[i]
if (mod.title == selectedNormalModifier.toppingName) {
modIndex = i
}
}
itemToEdit.modifierGroups.modifiers[modIndex].isItemSelected = true
mSelectedNormalModifiers.add(selectedNormalModifier)
Log.e(TAG, "how many times did we get here $modifierGroupModel")
}
As you can see I am being very specific on the item that I want to edit. Regardless of this, the item at index 2 also gets edited and vice versa.
This is how I duplicate the items
for (i in modifierGroupModel!!.itemModifierGroups.indices) {
val item = modifierGroupModel!!.itemModifierGroups[i]
// only do this if the display count is greater than one
if (item.modifierGroups.displayCount.toInt() > 1) {
for(i in 0 until item.modifierGroups.displayCount.toInt()){
val localIndex = i + 1
item.itemIndex = localIndex
item.itemLocalTitle = getNumberOfName(localIndex) + " " + item.modifierGroups.modifierGroupTitle
tempItemModifierGroupModel.add(if (i > 0) item.copy() else item)
}
} else {
item.itemIndex = i
tempItemModifierGroupModel.add(item)
}
}
val newModidiferGroupModel = ModifierGroupsModel(
itemID = modifierGroupModel!!.itemID,
itemName = modifierGroupModel!!.itemName,
itemModifierGroups = ArrayList(tempItemModifierGroupModel.toMutableList())
)
modifierGroupModel = newModidiferGroupModel
The JSON object looks like this
"item" {
"nested list"[
"isSelected": "false"
]
},
"item" {
"nested list"[
"isSelected": "false" // when i change this to true
]
},
"item" {
"nested list"[
"isSelected": "false" // this one changes as well
]
}
]```
I'm guessing because you didn't show your Item data class but it looks like you are not editing the item in your list, but rather some indirectly referenced object. See this line:
itemToEdit.modifierGroups.modifiers[modIndex].isItemSelected = true
itemToEdit is not getting modified. Some indirectly referenced object in a collection called modifiers is what you're modifying.
When you copy an Item, it only copies all the property values. For a non-primitive property, the value is a reference to specific instance of a class. It does not perform a "deep copy". So your items at index 1 and 2 are different objects, but they reference the same instance of whatever is in the modifierGroups property.
I found the answer. ---> this other Stack Overflow question answered it. dataclass copy with field ArrayList - change the ArrayList of the copied class changes the original
The user #Andrey_yog
I have a list of 30 random numbers that correspond to 1 of 8 colours, and I need to iterate over the 8 colors(or 30 numbers) and find the number of times each colour occurs. I need to do this using lambdas and functional programming, so no traditional for loops.
val iterator = colours.toList().iterator()
iterator.forEach{
println("$it count: " + (numbers
.map{a -> colours[a]}
.count{it == ("$it")}))
}
The problem currently is my output for count is just 50, not the specific number of times a colour occurs.
If I do it like this:
println("Red count:" + (numbers
.map{a -> colours[a]}
.count{it == ("red")}))
it outputs the correct number, but not with the loop.
What it ouputs:
green count: 50
red count: 50
what it should output (for example)
green count:9
red count:3
Thanks in advance
Add a named parameter to your forEach loop. The implicit name "it" is getting shadowed by the count function.
val iterator = colours.toList().iterator()
iterator.forEach { colour ->
println("$colour count: " + (numbers
.map{a -> colours[a]}
.count{it == ("$colour")}))
}
You don't really need to do a nested iteration here. Currently you're operating at O(n^2) since you have to traverse the list once for every element. Since you know you're working with a small number of potential values, you could instead just group them by value and then map the values to the size of the resulting lists, i.e.
val colourNames = listOf("red", "green", "blue", "yellow", "orange", "indigo", "violet", "black")
// Generates 30 random numbers between 0 and 8 (exclusive)
val randomColours = (0 until 30).map { (0 until colourNames.size).random() }
val result = randomColours
.groupBy { color -> colourNames[color] } // outputs a Map<String, List<Int>>
.mapValues { (color, colorCountList) -> colorCountList.size } // Map<String, Int>
println(result) // {yellow=4, orange=4, red=5, indigo=3, blue=8, green=2, violet=2, black=2}
I am working on an Android application that uses a Google Spreadsheet as a database.
The application should GET, APPEND and UPDATE values in a spreadsheet, using the Sheets API v4. The first two functions are working fine but I have difficulties updating a specific row. I need to find a row that has a specific value in it's first column ("Batch ID") and update all the cells in this row.
This is how my spreadsheet looks like.
Right now I am getting the row to be modified like this:
ValueRange response = this.mySheetsService.spreadsheets().
values().get(spreadsheetId, range).execute();
List<List<Object>> values = response.getValues();
String rangeToUpdate;
Log.i(TAG, "all values in range: " + values.toString());
int i = 0;
if (values != null) {
for (List row : values) {
i += 1;
if (row.get(0).equals(selectedBatchID)) {
Log.i(TAG, "IT'S A MATCH! i= " + i);
rangeToUpdate = "A" + (i + 1) + ":E" + (i + 1); //row to be updated
}
}
}
/*once I have the row that needs to be updated, I construct my new ValueRange requestbody and
*execute a values().update(spreadsheetId, rangeToUpdate , requestbody) request.
*/
This is actually working fine but I think it's an ugly solution and I am sure there is a better one out there.
I have read the Sheets API documentation and I got familiar with notions such as batchUpdateByDataFilter, DataFilterValueRange or DeveloperMetadata and I sense that I should use these features for what I'm trying to achieve but I couldn't put them together and I couldn't find any examples.
Can someone show me or help me understand how to use these Sheets V4 features?
Thank you.
I have exactly the same issue, and it seems that so far (March 2018) Sheets v4 API does not allow to search by value, returning cell address. The solution I found somewhere here on StackOverflow is to use a formula. The formula can be created in an arbitrary sheet each time you want to find the address by value, then you erase the formula. If you do not want to delete the formula every time, you many prefer to create in a safer place, like a hidden worksheet.
Create hidden worksheet LOOKUP_SHEET (spreadsheetId is your spreadsheet ID):
POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
{
"requests": [
{
"addSheet": {
"properties": {
"hidden": true,
"title": "LOOKUP_SHEET"
}
}
}
]
}
Create a formula in the A1 cell of the hidden worksheet that searches for "Search value" in MySheet1 sheet, and get back the row:
PUT https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/LOOKUP_SHEET!A1?includeValuesInResponse=true&responseValueRenderOption=UNFORMATTED_VALUE&valueInputOption=USER_ENTERED&fields=updatedData
{
"range": "LOOKUP_SHEET!A1",
"values": [
[
"=MATCH("Search value", MySheet1!A:A, 0)"
]
]
}
The response will look like this:
{
"updatedData": {
"range": "LOOKUP_SHEET!A1",
"majorDimension": "ROWS",
"values": [
[
3
]
]
}
}
By default, major dimension is ROWS. MATCH() returns relative row within column A, if no row IDs are provided then this position is effectively absolute. Or, you may want to use a more reliable call like =ROW(INDIRECT(ADDRESS(MATCH("Search value",A:A,0),1))). If the sheet has spaces in it, enclose it in single quotes. If you are searching for number, make sure you do not enclose it in quotes.
In the spreadsheets API we have the concept of developer metadata, that allow us to store information not visible to the end user that we can later on retrieve and use.
In this case the best approach is to assign the Batch ID as a metadata for a particular row. I will add the code based on the Javascript SDK.
const response = await sheets.spreadsheets.developerMetadata.search({
auth: jwtClient,
spreadsheetId,
requestBody: {
dataFilters: [
{
developerMetadataLookup: {
locationType: 'ROW',
metadataKey: 'batchId',
metadataValue: '$BATCH_ID'
}
}
]
}
});
if (response.matchedDeveloperMetadata) {
// There is a row with that id already present.
const { endIndex } = response.matchedDeveloperMetadata[0].developerMetadata.location.dimensionRange;
// Use endIndex to create the range to update the values range: `SheetName!A${endIndex}`,
await sheets.spreadsheets.values.update(
{
auth: jwtClient,
spreadsheetId,
range: `SheetName!A${endIndex}`,
valueInputOption: 'USER_ENTERED',
requestBody: {
majorDimension: 'ROWS',
values: [[]]
},
},
{}
);
} else {
// Append the value and create the metadata.
const appendResponse = await sheets.spreadsheets.values.append(
{
auth: jwtClient,
spreadsheetId,
range: 'SheetName!A1',
valueInputOption: 'USER_ENTERED',
requestBody: {
majorDimension: 'ROWS',
values: [[]]
},
},
{}
);
if (appendResponse.data.updates?.updatedRange) {
const updatedRange = appendResponse.data.updates?.updatedRange;
const [, range] = updatedRange.split('!');
const indexes = convertSheetNotation(range);
await sheets.spreadsheets.batchUpdate({ auth: jwtClient, spreadsheetId, requestBody: {
requests: [
{
createDeveloperMetadata: {
developerMetadata: {
location: {
dimensionRange: {
sheetId: 0,
startIndex: indexes[0],
endIndex: indexes[0] + 1,
dimension: "ROWS"
}
},
metadataKey: 'batchId',
metadataValue: '$BATCH_ID',
visibility: "DOCUMENT"
}
}
}
]
}});
}
}
We need to be careful of race conditions as we may end up with duplicated rows, let me know if that helps :)
I had the same requirement.
First:
Create a function that gets the index of targeted object from the sheet, like:
private int getRowIndex(TheObject obj, ValueRange response) {
List<List<Object>> values = response.getValues();
int rowIndex = -1;
int i = 0;
if (values != null) {
for (List row : values) {
i += 1;
if (row.get(1).equals(obj.getBatchId())) {
System.out.println("There is a match! i= " + i);
rowIndex = i;
}
}
}
return rowIndex;
}
Second:
Create the update method by passing the targeted object having your desired value "batch id" and others new values for the rest of fields.
public void updateObject(Object obj) throws IOException, GeneralSecurityException {
sheetsService = getSheetsService();
ValueRange response = sheetsService.spreadsheets().
values().get(SPREADSHEET_ID, "Sheet1").execute();
int rowIndex = this.getRowIndex(obj, response);
if (rowIndex != -1) {
List<ValueRange> oList = new ArrayList<>();
oList.add(new ValueRange().setRange("B" + rowIndex).setValues(Arrays.asList(
Arrays.<Object>asList(obj.getSomeProprty()))));
oList.add(new ValueRange().setRange("C" + rowIndex).setValues(Arrays.asList(
Arrays.<Object>asList(obj.getOtherProprty()))));
//... same for others properties of obj
BatchUpdateValuesRequest body = new BatchUpdateValuesRequest().setValueInputOption("USER_ENTERED").setData(oList);
BatchUpdateValuesResponse batchResponse;
batchResponse sheetsService.spreadsheets().values().batchUpdate(SPREADSHEET_ID, body).execute();
} else {
System.out.println("the obj dont exist in the sheet!");
}
}
Finally:
In your app you have to pass the tageted object to the update method:
TheObject obj = new Object();
obj.setBatchId = "some value";
Fill the obj with others values if you want.
Then call the method:
objectChanger.updateObject(obj);
All you need to do is to create a new array of strings from an array of arrays - so you can run the indexOf() method on this new array.
Since we know the method values.get returns array of the arrays such as
[
[""],
[""],
...
]
my approach was to a bit flattened this structure.
const data = await googleSheetsInstance.spreadsheets.values.get({
//here u have to insert auth, spreadsheetId and range
});
//here you will get that array of arrays
const allData: any[] = data.data.values;
//Now you have to find an index in the subarray of Primary Key (such as
//email or anything like that
const flattenedData = allData.map((someArray: any[]) => {
return someArray[2]; //My primary key is on the index 2 in the email
Array
});
So what you will get is a normal array of strings with primary keys so now you can easily call the indexOf() on the flattened array.
const index:number = flattenedData.indexOf("someuniquestring);
console.log(index);
And the index value will tell you the row. Do not forget spreadsheets start from 1 and indexes in Javascript start from 0.
How to get the last row with value in the new Google Sheets API v4 ?
i use this to get a range from a sheet:
mService.spreadsheets().values().get("ID_SHEET", "Sheet1!A2:B50").execute();
how to detect the last row with value in the sheet ?
You can set the range to "A2:D" and this would fetch as far as the last data row in your sheet.
I managed to get it by counting the total rows from current Sheets.
Then append the data to the next row.
rowcount = this.mService.spreadsheets().values().get(spreadsheetId, range).execute().getValues().size()
Rather than retrieving all the rows into memory just to get the values in the last row, you can use the append API to append an empty table to the end of the sheet, and then parse the range that comes back in the response. You can then use the index of the last row to request just the data you want.
This example is in Python:
#empty table
table = {
'majorDimension': 'ROWS',
'values': []
}
# append the empty table
request = service.spreadsheets().values().append(
spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME,
valueInputOption='USER_ENTERED',
insertDataOption='INSERT_ROWS',
body=table)
result = request.execute()
# get last row index
p = re.compile('^.*![A-Z]+\d+:[A-Z]+(\d+)$')
match = p.match(result['tableRange'])
lastrow = match.group(1)
# lookup the data on the last row
result = service.spreadsheets().values().get(
spreadsheetId=SPREADSHEET_ID,
range=f'Sheetname!A{lastrow}:ZZ{lastrow}'
).execute()
print(result)
😢 Google Sheets API v4 does not have a response that help you to get the index of the last written row in a sheet (row that all cells below it are empty). Sadly, you'll have to workaround and fetch all sheet rows' into memory (I urge you to comment if I'm mistaken)
Example:
spreadsheet_id = '1TfWKWaWypbq7wc4gbe2eavRBjzuOcpAD028CH4esgKw'
range = 'Sheet1!A:Z'
rows = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id, range=range).execute().get('values', [])
last_row = rows[-1] if rows else None
last_row_id = len(rows)
print(last_row_id, last_row)
Output:
13 ['this', 'is ', 'my', 'last', 'row']
💡 If you wish to append more rows to the last row, see this
You don't need to. Set a huge range (for example A2:D5000) to guarantee that all your rows will be located in it. I don't know if it has some further impact, may be increased memory consumption or something, but for now it's OK.
private List<String> getDataFromApi() throws IOException {
String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
String range = "A2:D5000";
List<String> results = new ArrayList<String>();
ValueRange response = this.mService.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values != null) {
results.add("Name, Major");
for (List row : values) {
results.add(row.get(0) + ", " + row.get(3));
}
}
return results;
}
Look at the loop for (List row : values). If you have two rows in your table you will get two elements in values list.
Have a cell somewhere that doesn't interfere with your datarange with =COUNTA(A:A) formula and get that value.
In your case
=MAX(COUNTA(A:A50),COUNTA(B:B50))
?
If there could be empty cells inbetween the formula would be a little more tricky but I believe it saves you some memories.
2022 Update
I I don’t know if this will be relevant for someone in 2022, but now you can do it differently.
You can just set next value as range :
const column = "A"
const startIndex = 2
const range = column + startIndex + ":" + column
In resolve you get all data in column and range with last index.
I tested it on js and php
Following Mark B's answer, I created a function that performs a dummy append and then extracts the last row info from the dummy append's response.
def get_last_row_with_data(service, value_input_option="USER_ENTERED"):
last_row_with_data = '1'
try:
dummy_request_append = service.spreadsheets().values().append(
spreadsheetId='<spreadsheet id>',
range="{0}!A:{1}".format('Tab Name', 'ZZZ'),
valueInputOption='USER_ENTERED',
includeValuesInResponse=True,
responseValueRenderOption='UNFORMATTED_VALUE',
body={
"values": [['']]
}
).execute()
a1_range = dummy_request_append.get('updates', {}).get('updatedRange', 'dummy_tab!a1')
bottom_right_range = a1_range.split('!')[1]
number_chars = [i for i in list(bottom_right_range) if i.isdigit()]
last_row_with_data = ''.join(number_chars)
except Exception as e:
last_row_with_data = '1'
return last_row_with_data
I m working on couchbase lite android.
I have series of documents, every document contains a field which it's value is array of string.
now I want to filter value of this field.
{
type : "customer",
name: "customerX",
states: [ "IL" , "IO" , "NY" , "CA" ]
},
{
type : "customer",
name: "customerY",
states: [ "WY" , "CA", "WA" ]
},
{
type : "customer",
name: "customerZ",
states: [ "NY" ]
}
I want to get customer documents which have "CA" in their states field.
I m using CBL Android.
emit(states , null);
then how could I make my Start and End Key option.
startkey("CA")
Or
startKey(["CA"])
customerX customerY
how can i get only customerX and customerY by "CA" in their states field ?!
If you are just wanting to filter on one state then your best solution would be to make a view like this:
function (doc, meta) {
if(doc.type && doc.type == "customer") {
if(doc.states) {
for (index = 0, len = doc.states.length; index < len; ++index) {
emit(doc.states[index],null);
}
}
}
}
This view will emit a view row for each state in the arrays of each of your documents of type 'customer'. This means that you can then select an individual state with a startkey of "CA" and an endkey of "CA", remember to set the inclusive_true flag to true so that endkeys that match are included:
startkey="CA"&endkey="CA"&inclusive_end=true
Hope that helps!