Google Spreadsheet API : Empty all cells in sheet - java

We use Google Sheet API v4. We want to clear entire sheet with empty data. We don't want to delete rows/columns.
Not working UpdateCells Call(Delete Columns by API) :
developers.google.com
Working UpdateCells Call(All Cells) :
developers.google.com

I was able to do it by using the clear method documented here looking at rev607.
ClearValuesRequest clearValuesRequest = new ClearValuesRequest();
// assumes you have a sheetservice initialized
sheetsService.spreadsheets().values().clear(spreadsheetId, "Sheet1", clearValuesRequest);
The key insight that wasn't well documented is you can pass in just the sheet name for the range and that clears ALL cells in the sheet.

Here's the Java Quickstart for Spreadsheet API.
You will then be using spreadsheets.batchUpdate to clear the sheets.Leave fields blank and place an asterisk to instruct Sheets API that all cells should be empty/cleared.
The following body request looks like:
{
"requests": [
{
"updateCells": {
"range": {
"sheetId": 0
},
"fields": "*"
}
}
]
}
Give this a quick-try in the oauth playground.
UPDATE: This is working now. It cleared my spreadsheet.

I don't know if this will help anyone else, but this is how I got it working for me:
This should clear your entire sheet.
I do not recall why 'sheetId' is 0, but perhaps it is an index into the sheets within the sheet itself. (like how you have tabs?)
I honestly do not remember though.
The function expects you to pass in your $service object and the spreadsheetID.
function clearSheet($service, $spreadsheetID = 0){
$request = new \Google_Service_Sheets_UpdateCellsRequest([
'updateCells' => [
'range' => [
'sheetId' => 0
],
'fields' => "*" //clears everything
]
]);
$requests[] = $request;
$requestBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$requestBody->setRequests($requests);
$response = $service->spreadsheets->batchUpdate($spreadsheetID, $requestBody);
return $response;
}

Java example:
UpdateCellsRequest clearAllDataRequest = new UpdateCellsRequest();
int allSheetsId = 0;
String clearAllFieldsSpell = "*";
GridRange gridRange = new GridRange();
gridRange.setSheetId(allSheetsId);
clearAllDataRequest.setRange(gridRange);
clearAllDataRequest.setFields(clearAllFieldsSpell);
BatchUpdateSpreadsheetRequest request = new BatchUpdateSpreadsheetRequest();
Request clearAllDataRequest = new Request().setUpdateCells(clearAllDataRequest);
request.setRequests(List.of(clearAllDataRequest));
return request;

Related

Reindex selected _source fields using Rest high level client in java

I want to re_index only selected fields from my document in elasticsearch using Rest High level client.
I know the elasticsearch query to achieve that but I don't know it's equivalent query using rest client.
Following is the elasticsearch query which I am trying to implement using rest client -
{
"body" : {
"source" : {
"index" : "my source index name",
"_source" : "id, name, rollNo"
},
"dest" : {
"index" : "my destination index name"
}
}
}
To write its equivalent query using rest client in java, I have used the following code -
ReindexRequest reindexRequest = new ReindexRequest();
reindexRequest.setSourceIndices("source index name").setDestIndex("destination index name");
reindexRequest.setDocTypes("id", "name", "rollNo", "_doc");
client.reindex(reindexRequest,RequestOptions.DEFAULT);
But the above code is not working as expected. It's re_indexing all the fields of my document. I want only selective 3 fields to be re_indexed from each doc.
You need to use below code as setDocTypes is not used for source filtering.
As there is no direct method available for setting source filter so you need to change underlying search request suing below code.
ReindexRequest reindexRequest = new ReindexRequest();
reindexRequest.setSourceIndices("source index name").setDestIndex("destination index name");
reindexRequest.setDocTypes("_doc");
String[] include=new String[] {"id", "name", "rollNo"};
String[] exclude=new String[] {"test"};
reindexRequest.getSearchRequest().source().fetchSource(include, exclude);
client.reindex(reindexRequest,RequestOptions.DEFAULT);

How to create a deep insert structure using Olingo Client?

How to POST the following JSON structure using Apache OLINGO client? What is the best way to build up this structure? Are there any examples?
{
"itemNumber": "ITEM1"
"lines": [
{
componentNumber": "COMPONENT1"
},
{
componentNumber": "COMPONENT2"
}
]
}
The following Java example using the Olingo Client works for me to post the following JSON structure:
{
"itemNumber": "ITEM1"
"lines": [
{
componentNumber": "COMPONENT1"
},
{
componentNumber": "COMPONENT2"
}
]
}
Java code using OLINGO CLient
public void deepInsertExample(){
//Initiate the ODATA client
ODataClient client = ODataClientFactory.getClient();
client.getConfiguration();
getClient().getObjectFactory();
//Initiate the Client Object Factory
ClientObjectFactory factory = getClient().getObjectFactory();
//Create Line Item 1
ClientEntity lineItem1 = factory.newEntity(new FullQualifiedName("ODATA.LineItem"));
lineItem1.getProperties()
.add(factory.newPrimitiveProperty("componentNumber", factory.newPrimitiveValueBuilder().buildString("COMPONENT2")));
//Create Line Item 2
ClientEntity lineItem2 = factory.newEntity(new FullQualifiedName("ODATA.LineItem"));
lineItem2.getProperties()
.add(factory.newPrimitiveProperty("componentNumber", factory.newPrimitiveValueBuilder().buildString("COMPONENT1")));
//Initiate the entity set
ClientEntitySet entitySet = factory.newEntitySet();
//Add Line Item 1 and Line Item 2 to the Enity
entitySet.getEntities().add(lineItem1);
entitySet.getEntities().add(lineItem2);
//Create the Lines LInk
ClientLink linesLink = factory.newDeepInsertEntitySet("Lines", entitySet);
ClientComplexValue complexValueCreate = factory.newComplexValue("Lines");
complexValueCreate.getNavigationLinks().add(linesLink);
//Create the Item object
ClientEntity item = factory.newEntity(new FullQualifiedName("ODATA.Item"));
item.getProperties()
.add(factory.newPrimitiveProperty("itemNumber", factory.newPrimitiveValueBuilder().buildString("ITEM1")));
//Add the Lines(Entity Set) link to Item Object
item.addLink(linesLink);
//Post the Item
URI absoluteUri = client.newURIBuilder("URL").build();
ODataEntityCreateRequest<ClientEntity> request = client.getCUDRequestFactory()
.getEntityCreateRequest(absoluteUri, item);
request.setAccept("application/json;odata.metadata=minimal");
request.execute();
}
You will have to specify the the NavigationPropertyName in the deep part. So for your sample payload it would look like
{
"itemNumber": "ITEM1",
"lines": {
"componentNumber":"COMPONENT1",
"componentNumber":"COMPONENT2",
}
}
You can refer to this post in SO to get details about n level nesting
The above answer asuumes that your NavigationProperty is named lines, you can substitute it with the right name by looking at service/$metadata
The answer assumes that you are trying to do deep inserts in a odata2 service, for OData4 the concept remains the same but syntax might vary a bit. Please refer to the payload descripted in documentation in case of OData 4

Mention channel using ms graph api

I am working with ms graph and i need to integrate with ms teams. In one case i have to send a message to a channel which will contain a channel mention (#). Currently i have found how to tag a specific user using java but i cant figure out how to mention the whole channel. It is posible to do so in ms graph and if yes how?
To be more specific here is an example. Assume that i have a team named TestTeam and this team has a channel named testChannel. i need to send a message where will contain #testChannel and will send a notification to everyone in this channel.
Also in slack api i am able to do this action by using "<!channel>" in my message.
This is the code i am using in order to tag a user:
static void send_message(String text, String mentionName, String channelName, String teamName, String accessToken) {
ensureGraphClient(accessToken);
Team team = getTeam(accessToken, teamName);
Channel channel = getChannel(accessToken, channelName, team);
User user = getUser(accessToken, mentionName);
if (userInChannel(user, team, channel)) {
ChatMessage chatMessage = new ChatMessage();
ItemBody body = new ItemBody();
body.contentType = BodyType.HTML;
body.content = String.format("%s. <at id=\"1\">%s</at> ", text, mentionName);
chatMessage.body = body;
ChatMessageMention m = new ChatMessageMention();
m.id = 1;
IdentitySet st = new IdentitySet();
Identity ide = new Identity();
ide.id = user.id;
ide.displayName = user.displayName;
st.user = ide;
m.mentioned = st;
m.mentionText = mentionName;
List<ChatMessageMention> cmmt = new LinkedList<>();
cmmt.add(m);
chatMessage.mentions = cmmt;
graphClient.teams(team.id).channels(channel.id).messages()
.buildRequest()
.post(chatMessage);
}
}
I also tried something similar in order to tag channels but it did not worked:
body.contentType = BodyType.HTML;
body.content = String.format("%s. <at id=\"1\">%s</at> ", text, "myChannel");
chatMessage.body = body;
ChatMessageMention m = new ChatMessageMention();
m.id = 1;
IdentitySet st = new IdentitySet();
Identity ide = new Identity();
ide.id = "19%xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%40thread.tacv2";
ide.displayName = "myChannel";
st.user = ide;
m.mentioned = st;
m.mentionText = "myChannel";
List<ChatMessageMention> cmmt = new LinkedList<>();
cmmt.add(m);
chatMessage.mentions = cmmt;
graphClient.teams(team.id).channels(channel.id).messages()
.buildRequest()
.post(chatMessage);
Introduction:
After playing around with it, here is the final result. And this is not final answer, but hope to lead for solution.
Lets start some introduction.
First lets define our post url:
POST https://graph.microsoft.com/beta/teams/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/channels/19:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#thread.tacv2/messages
Now lets mentioning a user in graph api:
{
"body": {
"contentType": "html",
"content": "Hello World <at id=\"0\">Maytham Fahmi</at>"
},
"mentions": [
{
"id": 0, <- the index here should be the same as in the content message
"mentionText": "Maytham Fahmi",
"mentioned": {
"user": {
"displayName": "Maytham Fahmi",
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" <- this should be user user object id
}
}
}
]
}
This is tested and working via Graph Api.
Now I come to the interested part. Sending to channel with channel mentioning looks like this:
{
"body": {
"contentType": "html",
"content": "Hello World <at id=\"0\">General</at>"
},
"mentions": [
{
"id": 0, <- the index here should be the same as in the content message
"mentionText": "General",
"mentioned": {
"conversation": {
"id": "19:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#thread.tacv2",
"displayName": "General",
"conversationIdentityType#odata.type": "#Microsoft.Teams.GraphSvc.conversationIdentityType",
"conversationIdentityType": "channel"
}
}
]
}
The results in teams, that is sent from channel mentioning
This is also working from Graph api explorer.
The answer:
So as we can see for user mentioning we use User object under IdentitySet instance which works fine for user. I have testing it C# using Microsoft.Graph.
Here is comes the problem Conversation object does not exist under IdentitySet in C# using Microsoft.Graph. I am pretty sure this is the case for Java as well. If you have that object in Java then you should be able to make it. It is perhaps not in C#.
I have red different places, and sadly channel mentioning objects is not implemented in code yet, looks like a bug or a feature they will fix over time.
A work around solution is, you have graph api json payloads, you can make a Java client to post Jsons to the url with and your job is done.
Note: I have look at our solution, it does not support mentioning as well.
Inspiration links:
https://learn.microsoft.com/en-us/graph/api/channel-post-message?view=graph-rest-1.0&tabs=csharp#permissions
https://learn.microsoft.com/en-us/graph/api/resources/chatmessagemention?view=graph-rest-1.0
Note in case you need to send notification in general, you could use webhooks.

AWS textract Extract the meta-data and confidence score

Hi all i have extracted the document meta-data from AWS texttract Asynchronous call using java SDK but the meta-data is segregated into multiple blocks and it's huge.
How to extract the confidence score, value and its field name separately using java code i want to extract result something like below:
[{
"Field" : "FirstName",
"Value" : "XXXXX",
"confidence Score" : "98.88"
},
{
"Field" : "LastName",
"Value" : "XXXXX",
"confidence Score" : "65.98"
}]
Could anyone please suggest how to extract the field,value and its confidence score from aws texttract document meta-data?
anyone having any idea on this?
AWS has provided an example for mapping key and value pairs in python. You can use this code to understand the logic and come up with your own code in JAVA.
Source: https://docs.aws.amazon.com/textract/latest/dg/examples-extract-kvp.html
I have just begun with AWS Textract too in Java and wow what a great tool ! I have included code in my answer at this link if you would like to take a look :)
It extracts the keys and values. I suggest you create a model with Key, Value and confidence scores and then create an object for each key value pair
public static ArrayList<KVPair> getKVObjects(List<Block> keyMap, List<Block> valueMap, List<Block> blockMap ) {
ArrayList<KVPair> labelValues = new ArrayList<>();
Block value_block;
for (Block key_block : keyMap) {
value_block = findValueBlock(key_block, valueMap);
String key = getText(key_block, blockMap);
Float top = value_block.getGeometry().getBoundingBox().getTop();
Float left = value_block.getGeometry().getBoundingBox().getLeft();
Float confidenceScore = value_block.getConfidence();
Optional<KVPair> label= (labelValues.stream().filter(x-> x.getLabel().equals(key)).findFirst());
Property property = new Property();
property.setValue(getText(value_block, blockMap));
property.setLocationLeft(left);
property.setLocationTop(top);
property.setConfidenceScore(confidenceScore);
if(label.isPresent()){
label.get().setProperties(property);
}else{
KVPair KVPair = new KVPair();
KVPair.setLabel(key);
KVPair.setProperties(property);
labelValues.add(KVPair);
}
}
return labelValues;
}
AWS-Textract-Key-Value-Pair Java - thread "main" java.lang.NullPointerException

Get Value from JSON String generated from C# WebService

This is my C# webservice which generates a JSON String. The below code block is what im using for that.
List<Dictionary<String, Object>> lstdict = new List<Dictionary<String, Object>>();
...
...
...
Logic for connecting db and getting records in msqldat (data reader)
goes here.
...
...
while (msqldat.Read())
{
var detls = new Dictionary<string, object>();
for (int i = 0; i < msqldat.FieldCount; i++)
{
detls.Add(msqldat.GetName(i), msqldat.IsDBNull(i) ? null :
msqldat.GetValue(i));
lstdict.Add(detls);
}
}
JavaScriptSerializer jss = new JavaScriptSerializer();
String mret = jss.Serialize(lstdict);
The above webservice is called in a java code from android studio and it returns the below String.
{"GetDataResult":"[
{\"uname\":\"hkIUZIikXVTC5aNaSva8IQ==\",
\"passwd\":\"hkIUZIikXVTC5aNaSva8IQ==\",
\"validupto\":\"\\\/Date(1545330600000)\\\/\",
\"dept\":\"juubHSHgLr\/3JWnrZCh5LeeW5Q7lioWOZ1\/Tg+YRy\/o=\",
\"rid\":1},
{\"uname\":\"hkIUZIikXVTC5aNaSva8IQ==\",
\"passwd\":\"hkIUZIikXVTC5aNaSva8IQ==\",
\"validupto\":\"\\\/Date(1545330600000)\\\/\",
\"dept\":\"juubHSHgLr\/3JWnrZCh5LeeW5Q7lioWOZ1\/Tg+YRy\/o=\",
\"rid\":2}]"}
I am trying to get the values in android application by using this Java code :
JSONObject uiobj = new JSONObject(mret);
JSONArray arrUserinfo = uiobj.getJSONArray("GetDataResult");
arrUserinfo.getJSONObject(0).getString("uname"))
The code fails at the second line. I'm new to JSON. Not sure if the JSON generated from c# code is not right or java code for parsing is not right. Please advise further. Thank you in advance.
The above json is serialized you need to parse json and then extract the object from it.
See this json valid..
{
"GetDataResult": [{
"uname": "hkIUZIikXVTC5aNaSva8IQ==",
"passwd": "hkIUZIikXVTC5aNaSva8IQ==",
"validupto": "/Date(1545330600000)/",
"dept": "juubHSHgLr/3JWnrZCh5LeeW5Q7lioWOZ1/Tg+YRy/o=",
"rid": 1
},
{
"uname": "hkIUZIikXVTC5aNaSva8IQ==",
"passwd": "hkIUZIikXVTC5aNaSva8IQ==",
"validupto": "/Date(1545330600000)/",
"dept": "juubHSHgLr/3JWnrZCh5LeeW5Q7lioWOZ1/Tg+YRy/o=",
"rid": 2
}
]
}
To parse json in java see below post..
https://www.geeksforgeeks.org/parse-json-java/

Categories