How to add new google spreadsheet and update using javascript - javascript

Im able to get the spreadsheet data from the below reference
https://developers.google.com/sheets/quickstart/js#step_3_run_the_sample
but I didn't found any sample for creating or updating spreadsheet from javascript.
How can I create a new spreadsheet and update it using javascript v4 google API.

Create a spreadsheet using Spreadsheet API:
Make an HTTP requests like:
POST https://sheets.googleapis.com/v4/spreadsheets
This POST request comes with a request body. The request body looks something like:
{
"spreadsheetId": string,
"properties": {
object(SpreadsheetProperties)
},
"sheets": [
{
object(Sheet)
}
],
"namedRanges": [
{
object(NamedRange)
}
],
}
Don't forget to enable the following scopes:
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/spreadsheets
Try it in the oauth playground.
Updating Spreadsheets:
You'll be making use of batchUpdate. This method lets you update any of the spreadsheet details. Changes are grouped together in a batch so that if one request fails, none of the other (potentially dependent) changes is written. The batchUpdate method works by taking one or more Request objects, each one specifying a single kind of request to perform. There are many different kinds of requests. Here's a breakdown on the types of requests, grouped into different categories.
More of that in Updating Spreadsheets docs.
Format looks like:
POST .../v4/spreadsheets/spreadsheetId:batchUpdate
Request body:
{
"requests": [{
"updateSpreadsheetProperties": {
"properties": {"title": "My New Title"},
"fields": "title"
}
}]
}
Full sample here.

Related

How to call JSONRPC to Odoo API getting data from multiple models in a single request?

I am calling JSONRPC from an application using this code:
const res = await axios.post(server + '/jsonrpc',
{
"jsonrpc": "2.0",
"method": "call",
"params": {
"service": "object",
"method": "execute",
"args": [database, uid, password, model, "search_read", [domain], fields, 0, 0, orderBy],
"id": 1,
}
}
);
When I want to get data from 1 model, it works fine. However, when I want to get data related to this model, I currently have to do this call again to the destination model with the domain according to the related origin data. For example, I know the Sale Order id and I want to get Stock Pickings that belong to that specific Sale Order, I need to call this 3 times. Not to mention that I need many more other related fields, which requires the application to send multiple requests to the server.
I think there must be a way to put the models and domains together to send the request only once so that it is most efficient like any other query languages and tools can do.
The question is how to do it?

Limiting data based on document field in Firestore

I am playing around with Firebase Firestore for my data needs. I have a data model where there is a collection of apples, each document representing a unique apple which has a field of type object which is essentially a map of <string, string>
Think of each apple document like:
name: "SiberianApple",
weight: "400g"
color: {
today: "green",
yesterday: "red",
tomorrow: "crimson"
}
Reading through the docs, I understood that we can query collections in various ways, but is it possible to limit the amount of information a client needs to fetch while fetching a document. Can I have a query on this field color such that it only returns
name: "SiberianApple",
weight: "400g",
color: {
today: "green"
}
Basically something like graphql, where I can ask for what I want. Wanted to know if it's possible to query or should this field be a subcollection of this document so that I can query colors using path apples/<appleId>/colors for the subCollection?
Cloud Firestore listeners fire on the document level. There is no way to get triggered with just particular fields in a document or split the document to get only one property. It's the entire document or nothing. So the Firestore client-side SDKs always return complete documents. Unfortunately, there is no way to request only a part of the document.
is it possible to limit the amount of information.
As said, that's not possible. What you can do, is to create another collection with documents that can only hold the fields you need. This practice is called denormalization, and it's a quite common practice when it comes to NoSQL databases.
As Alex explained in his answer, with the Client SDKs it is not possible to get only a subset of the fields of a Document. When you fetch a Document you get it with all its fields.
However, this is possible with the Firestore REST API: With the REST API you can use a DocumentMask when you fetch one document with the get method. The DocumentMask will "restrict a get operation on a document to a subset of its fields".
For querying several documents, it is a bit different: you use the runQuery method. The request body shall contain a
JSON object which has a structuredQuery property.
In turn, the StructuredQuery has a select property which contains a Projection object.
Here is an example of a a payload used when calling the runQuery method (i.e. https://firestore.googleapis.com/v1/{parent=projects/*/databases/*/documents}:runQuery):
const payloadObj = {
structuredQuery: {
where: {
// ....
},
orderBy: [
{
field: {
fieldPath: 'name',
},
direction: 'ASCENDING',
},
],
from: [
{
collectionId: 'apples',
},
],
select: {
fields: [
{
fieldPath: 'name',
},
{
fieldPath: 'weight',
},
{
fieldPath: 'color.today',
},
],
},
limit: 1000,
},
};
As you can see in the code above, you can target only one of the properties of the color map, with fieldPath: 'color.today',
End note: The REST API is less user friendly than the client SDKs because you need to build the payload passed to the request and parse the responses but it is not difficult to use. In a web app, use fetch, axios or gaxios to execute the calls.

How to specify JSON request body example in Postman Collection

I'm programmatically creating Postman collections and want to provide a default JSON request body to make requests easier.
I've looked through the spec can can't see how to specify it. Does anyone have any ideas? Could this be done with JavaScript, which I've used to automatically set headers and environment variables.
Here's the Postman Collection spec definition I'm working with, v2.1.0 draft 4:
https://schema.getpostman.com/collection/json/v2.1.0/draft-04/collection.json
https://schema.getpostman.com/
A request is specified by #/definitions/request.
The body is specified as one of the following. JSON isn't listed so raw is typically used. Here's an excerpt which appears shows that raw is a string type but there's no property to include a default value for the request body.
{
"body":{
"oneOf":[
{
"type":"object",
"description":"This field contains the data usually contained in the request body.",
"properties":{
"mode":{
"description":"Postman stores the type of data associated with this request in this field.",
"enum":[
"raw",
"urlencoded",
"formdata",
"file",
"graphql"
]
},
"raw":{
"type":"string"
}
}
}
]
}
}
Is anyone aware of Postman being able to specify an example for the JSON request body with a pre-created example, using the collection directly or via JavaScript?
UPDATE
The following YouTube video shows on the body can be set dynamically with JavaScript using the following.
const body = {
"productId": 1234
};
pm.globals.set("body", JSON.Stringify(body));
https://www.youtube.com/watch?v=hSX7Dcjy000
Using this approach, it seems the next thing to figure out is if the Postman Collection can import and access custom properties, e.g. x-properties, or if there's some other way to load the example content by overloading an existing property. It seems this can be done by loading a lot of environment variables, one for each request. The final step may be to be to automatically load the correct environment variable value into the example body when the user first brings up the endpoint.
Here's more information on a similar topic:
https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#scripting-with-request-data
The link you added is scripting withing postman test and pre request section , not for programmatically creating json.
You can open postman and click the inverted hamburger menu of collection to export the collection json. You can use this as reference.
In the generated json request is defined as : (Only url , method and body part not full )
"method": "DELETE",
"header": [],
"body": {
"mode": "raw",
"raw": "{{requestbody}}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/resource1/resource2",
"host": [
"{{url}}"
],
"path": [
"resource1",
"resource2"
]
}
This is the json created from postman.

MS Graph create onlineMeeting: "onlinemeeting cannot be null."

I'm currently trying to create online meetings using the Graph Explorer and facing a similiar problem like in this question: onlinemeeting-cannot-be-null-error-when-creating-new-meeting-with-beta-api.
Whenever I'm sending a Post request to https://graph.microsoft.com/v1.0/me/onlineMeetings the answer is a 400 Bad Request error with message: onlinemeeting cannot be null. The body looks like the example from the official documentation:
{
startDateTime:"2020-09-09T14:33:30.8546353-07:00",
endDateTime:"2020-009-09T15:03:30.8566356-07:00",
subject:"test"
}
No matter what, I can't get this to work. Other endpoints work well and the API permissions fit the requirements. Any thoughts on this?
The participants should be provided in the onlineMeeting resource, participants is one of properties in onlineMeeting object, you can also add other properties of onlineMeeting.
So you need to add it to your request body, here is a sample for your reference:
{
"startDateTime":"2019-09-09T14:33:30.8546353-07:00",
"endDateTime":"2019-09-09T15:03:30.8566356-07:00",
"subject":"Application Token Meeting",
"participants": {
"organizer": {
"identity": {
"user": {
"id": "550fae72-d251-43ec-868c-373732c2704f"
}
}
}
}
}

Nested json data in Angular 2 - how to filter?

I'm new with Angular, Js etc, and have a problem with understanding how should work with nested data. For example:
I have four json files:
categories
subcategories
posts
comments
It's better to have 4 different files like above, or one like this:
{
"id_category":"1",
"name":"business",
"subcategories":{
"id":"1",
"name":"ecommerse",
"posts": {
"id": 1,
"title": "New post"
"comments": {
"id": 1,
"text": "text text text"
}
}
}
Of course it's only an example, but for that example I need to find comment by Id = 1 to get information about which post this comment is related to, which subcategory and category.
Now I have four different files, and services to get data from json files. I can get a specific comment by ID:
getComment(Id: number) {
return this.comments.find(
(comment) =>
comment.id === Id
);
}
ok, fine. But If I want to get information about post, subcategory, and main category for this comment? What should I do?
It depends on what the specific needs of your application are.
With this structure:
{
"id_category":"1",
"name":"business",
"subcategories":{
"id":"1",
"name":"ecommerse",
"posts": {
"id": 1,
"title": "New post"
"comments": {
"id": 1,
"text": "text text text"
}
}
}
You could iterate over Categories and display a list, then allow a user to select a single category, and assign that to var currentCategory.
You could then iterate over currentCategory.subcategories to allow the user to select a subcategory and assign that to var currentSubCategory. You would keep drilling down then into currentSubCategory.posts, allow the user to select a post, assign that to var currentPost and then iterate over that to display currentPost.comments.
If you're fetching from a database in order to allow the user to drill down into the data for display only, then something like this would work.
If you're maintaining data in JSON files, then I would look at something like JSON Server https://github.com/typicode/json-server
If you're building something more substantial and you have a database backend, make use of the database and don't try to recreate that functionality in your JSON, use JSON as a transport for the data, but don't try to replicate entire tables or complex data structures in your front end code, just fetch what you need as you need it, as that will make for a much more stable and scalable application, and will also make it easier for you do fetch and update data in small manageable chunks.
For mocking a data backend using JSON, consider json-server
https://www.npmjs.com/package/json-server

Categories

Resources