How to add content to table cell via google-docs-api request? - google-docs-api

I want to add a content to a table cell in google doc, but the way described in the documentation doesn't work.
What is wrong with my request? When I provide 1 for index parameter of insertText request it just pastes text before table. When I provide 2 as value of index parameter I get an error: "Invalid requests[1].insertText: The insertion index must be inside the bounds of an existing paragraph. You can still create new paragraphs by inserting newlines."
{
"requests": [
{
"insertTable": {
"endOfSegmentLocation": {
"segmentId": ""
},
"columns": 1,
"rows": 1
}
},
{
"insertText": {
"location": {
"index": 1
},
"text": "Cell content"
}
}
]
}
I expect that text must be inserted into the only cell of the table.

You want to append a table (1 x 1) to the last body.
You want to insert a text to the 1st cell.
From your request body, I could understand like above. If my understanding is correct, how about this flow? I think that there might be several solutions. So please think of this as just one of them.
In the case that new table is appended to the last body ("segmentId": "" means that the table is appended to the last body.), at first, the start index of the table is required to be known. So how about the following flows?
Flow 1:
In this flow, it supposes that the index of last body is not known.
Append a table using the following request body.
{
"requests": [
{
"insertTable": {
"endOfSegmentLocation": {
"segmentId": ""
},
"columns": 1,
"rows": 1
}
}
]
}
Retrieve the start index of table using the following endpoint. At that time, you can also retrieve the start index of the cell.
GET https://docs.googleapis.com/v1/documents/{fileId}?fields=body(content(startIndex%2Ctable))
Insert the text to the cell. In this case, it supposes that the retrieved start index of the appended table is 10. The start index of the 1st cell is 14 (I think that the start index of the 1st cell can be retrieved by start index of table + 4.). In this case, the request body for inserting the text to the cell is as follows.
{
"requests": [
{
"insertText":
{
"location":
{
"index": 14
},
"text": "Cell content"
}
}
]
}
Flow 2:
In this flow, it supposes that the index of last body is known. For example, when the table is appended to the new Document, you can create the table with the text using the following request body. In this case, the start index of the table and the cell are 1 and 5, respectively.
{
"requests": [
{
"insertTable":
{
"endOfSegmentLocation":
{
"segmentId": ""
},
"columns": 1,
"rows": 1
}
},
{
"insertText":
{
"location":
{
"index": 5
},
"text": "Cell content"
}
}
]
}
References:
Inserting or deleting table rows
Thread: Insert table in Google Docs API Python
If I misunderstood your question and this was not the direction you want, I apologize.

This is a follow up to the answers posted already. #ANewb hinted the the flow ends after the 5th cell. Ensure the values/entries in the payload aren't empty strings. For anyone encountering the same challenge, you can try using tenary conditions - for php $value == "" ? "N/A : $value;, for js let value = checkValue == "" ? "N?A": checkValue;

Related

Is there a way to use "Replace Named Range Content Request" on Table cell content?

I am trying to use the "Replace Named Range Content Request" to update the content of table cells but I keep getting a this range cannot be replaced error, I also tried deleting the table cells content,not the table cell itself, but I got this error (
{
"error": {
"code": 400,
"message": "Invalid requests[0].deleteContentRange: Invalid deletion range. Cannot delete the requested range.",
"status": "INVALID_ARGUMENT"
}
}
)
I have cross-checked the indexes I am using and I can't figure it out, Insert Text request works but replacing or deleting the text in a table cell brings back an error.
Here is the body of one of my table cells in JSON format
"startIndex": 145,
"endIndex": 152,
"paragraph": {
"elements": [
{
"startIndex": 145,
"endIndex": 152,
"textRun": {
"content": "Item 1\n",
"textStyle": {}
}
My delete content request in JSON format
{
"requests": [
{
"deleteContentRange": {
"range": {
"startIndex": 145,
"endIndex": 152
}
}
]
}
I created a named range with the table cell content start and end indexes and I was expecting it to automatically replace the text in the cell when I use "Replace Named Range Content Request"
I think this error normally appears when the range you specify attempts to delete an element outside of the current element's index range.
If you're trying to remove the word "Test" then endIndex should be 149 if starting at 145 (4 characters).
As the table cell itself is from range 145 to 152, then the delete range should be 146 to 151 to delete all the cell contents.

Restassured: How Can we compare each element in Json array to one particular Same value in Java using Hemcrest Matchers, not using Foreach loop

Restassured: How Can we compare each element in Json array to one particular Same value in Java using Hemcrest Matchers, not using Foreach loop.
{
"id": 52352,
"name": "Great Apartments",
"floorplans": [
{
"id": 5342622,
"name": "THE STUDIO",
"fpCustomAmenities": [
{
"displaySequence": 2,
"amenityPartnerId": "gadasd",
"display": true,
"leased": true
},
{
"displaySequence": 13,
"amenityPartnerId": "sdfsfd",
"display": true,
"leased": true
}
]
},
{
"id": 4321020,
"name": "THE First Bed",
"fpCustomAmenities": [
{
"displaySequence": 4,
"amenityPartnerId": "gadasd",
"display": true,
"leased": true
},
{
"displaySequence": 15,
"amenityPartnerId": "hsfdsdf",
"display": true,
"leased": true
}
]
}
]
}
I want to compare that Leased=true for all the leased nodes at all the levels in the json response...
I have working code...
List<List<Boolean>> displayedvaluesfpStandardAmenities =
when().get(baseUrl + restUrl).
then().statusCode(200).log().ifError().
extract().body().jsonPath().getList("floorplans.fpCustomAmenities.display");
for (List<Boolean> displayedStandardList : displayedvaluesfpStandardAmenities) {
for (Boolean isDisplayedTrue : displayedStandardList) {
softAssert.assertTrue(isDisplayedTrue);
}
}
But the issue is I need the code to be in simple format using either Hemcrest Matchers or Restaussred Matchers and try simplistic way like Below, ( which is not working)
when().get(baseUrl + restUrl).
then().assertThat().body("floorplans.fpCustomAmenities.display",equalTo("true"));
The error I am getting is
java.lang.AssertionError: 1 expectation failed.
JSON path floorplans.fpCustomAmenities.display doesn't match.
Expected: true
Actual: <[[true, true], [true, true]]>
So what I need is the that all thes 'display' nodes in the json response where ever it is need to compared with "true", so that my test can Pass.
I have an alternate solution like mentioned above, but All I need is working solution using matchers.
Assuming fpCustomAmenities arrays are not empty, you can use the following solution;
when().get(baseUrl + restUrl).then()
.body("floorplans.findAll { it }.fpCustomAmenities" + // 1st line
".findAll { it }.leased.each{ a -> println a }" + // 2nd line
".grep{ it.contains(false) }.size()", equalTo(0)); // 3rd line
Here from the 1st line, we return each object in fpCustomAmenities array.
From the 2nd line we get boolean value of leased in each fpCustomAmenities object to a boolean array ([true, true]).
Each boolean array is printed from .each{ a -> println a }. I added it only to explain the answer. It is not relevant to the solution.
From 3rd line we check whether, if there is a false in each boolean array. grep() will return only the arrays which has a false. And then we get the filtered array count. Then we check whether it is equal to 0.
Check groovy documentation for more details.
Or
This solution does not use any Matchers. But this works.
String responseBody = when().get(baseUrl + restUrl).
then().extract().response().getBody().asPrettyString();
Assert.assertFalse(responseBody.contains("\"leased\": false"));

Google sheets API append method (last on top)

When using google sheets api append method (in any language), the values to be appended to the sheet are added after the last non null row.
So new values appear at the bottom of the sheet, as explained here:
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#InsertDataOption
How can I append values in a way that the new values appear at the top of the sheet?
You want to append values by inserting new rows. If my understanding is correct, how about this method? It seems that sheets.spreadsheets.values.append appends values to the last row. So I would like to propose to usesheets.spreadsheets.batchUpdate. The endpoint and request body are as follows. When you use this, please modify ### spreadsheet ID ###, "sheetId": 1234567890 and the parameters for range and values.
Endpoint :
POST https://sheets.googleapis.com/v4/spreadsheets/### spreadsheet ID ###:batchUpdate
Request body :
{
"requests": [
{
"insertRange": {
"range": {
"sheetId": 1234567890,
"startRowIndex": 0,
"endRowIndex": 1
},
"shiftDimension": "ROWS"
}
},
{
"pasteData": {
"data": "sample1, sample2, sample3",
"type": "PASTE_NORMAL",
"delimiter": ",",
"coordinate": {
"sheetId": 1234567890,
"rowIndex": 0,
}
}
}
]
}
Flow of this request :
Insert new row to row 1 using "insertRange".
Import values of "sample1, sample2, sample3" using "pasteData".
When the order of "insertRange" and "pasteData" is changed, at first, the value of "A1:A3" is overwritten. After this, the new row is inserted to the row 1. So it seems that the elements of "requests" which is an array run in the order.
Reference :
sheets.spreadsheets.batchUpdate
If I misunderstand your question, I'm sorry.

how to display nested array data inside uitableview properly with Multiple level of Sections

I have the following json data
{
"Display_Selected List":
[
{
"product_name": "Product1",
"items":
[
{
"item_name": "SubItem1",
"specifications":
[
{
"list": [
{
"name": "Sp1"
},
{
"name": "Sp2"
}
],
"specification_name": "Specification Group 1"
},
{
"list": [
{
"name": "Sp3"
},
{
"name": "Sp4"
}
],
"specification_name": "Specification Group 2"
}
]
},
{
"item_name": "Sub Item2",
"specifications":
[
{
"list": [
{
"name": "Sp2"
}
],
"specification_name": "Specification Group 1"
},
{
"list": [
{
"name": "Sp3"
}
],
"specification_name": "Specification Group 2"
}
]
}
]
},
{
"product_name": "Product2",
"items":
[
{
"item_name": "Item1",
"specifications":
[
{
"list": [
{
"name": "Sp3"
},
{
"name": "Sp4"
}
],
"specification_name": "Specification Group 2"
}
]
}
]
}
]
}
As per the design requirement i have to diplay this whole data in single uitable view like follow
I have created rough design as shown in below image
I can achieve this via uitableview inside uitableviewcell but as
per Apple recommendation Apple does not recommend table views to be
added as subviews of other scrollable objects
Now my question is how can i achieve following design by single uitableview and and also as per my json all the content are dynamic
Does anyone have seen something like this around ? Any reference would be helpful.
If you don't wish to use tableView inside tableViewCell, you could possible go by the following approach.
Create 3 different cells first one for showing item name, second one for showing the Specification group name and the third one for showing the specification items (eg: Sp1,Sp2,..)
numberOfRowsInSection will have the correct count to show data using the above created cells. So numberOfRows should return the total count like rowsInSection =
count of items + count of specifications in each items + count of list in each specifications for each item
Change your data source accordingly and make condition check so that you will display the item Name cell first then followed by the cell for Specification group name then display specification items inside each specification then show the next item name and so on.
I hope this approach will help you achieve the result.
It will be easy if you could use tableView inside the tableViewCell, in many Applications I have used this approach and I haven't faced any Apple review problem. If you are using tableView inside tableViewCell it would be better to disable scrolling and bounces property.

Swagger query parameter template

I have on query parameter which is little bit complex and i have my own syntax to make that value. Its has more then one variable to make one complete string value.
Let suppose name of parameter is index which has row and column like to make this value 20:30
index = { row: 20, col:30 }
index2 = { row: 20, col:30, chr: 15 }
Now i wanted to make it as
example.com?index=20:30
example.com?index2=20:30:15
Can someone tell me how can i define this in swagger ?
Thank you.
Make your swagger parameter a string and in your code behind handle the splitting into multiple variables...
I do exactly that here:
http://turoapi.azurewebsites.net/swagger/ui/index#/Echo/Echo_Get
"parameters": [
{
"name": "location",
"in": "query",
"description": "SoFL= 26.16,-80.20",
"required": true,
"type": "string"
},
That location is (Latitude,Longitude) and I split it with a C# TypeConverter
...and the request looks like:
http://turoapi.azurewebsites.net/api/Echo?location=26.16,-80.20
The code for that WebApi is here:
https://github.com/heldersepu/TuroApi

Resources