These are the sample scripts for using getBatchGet, batchUpdateContacts, batchDeleteContacts, batchCreateContacts of People API with Google Apps Script.
When the data is retrieved and put with People API, the process cost can be reduced by using batch requests. Fortunately, in the current stage, People API has native methods for using batch requests. Those are getBatchGet, batchCreateContacts, batchDeleteContacts, and batchUpdateContacts. In this post, I would like to introduce the sample scripts for using the batch requests of People API.
These sample scripts are for Google Apps Script. Before you test these scripts, please enable People API at Advanced Google services.
I think that these sample scripts will help understand these batch request methods of People API. This will be also useful for creating the script in another language.
1. getBatchGet
// Please set this object for getting data.
const obj = {
resourceNames: ["people/###", , ,],
personFields: ["emailAddresses"],
};
const res = People.People.getBatchGet(obj);
console.log(res);
There is a maximum of 200 resource names.
2. batchCreateContacts
// Please set this object for creating data.
const obj = {
contacts: [
{
contactPerson: {
emailAddresses: [{ value: "### email address ###" }],
names: [
{ familyName: "sample familyName", givenName: "sample givenName" },
],
},
},
,
,
,
],
readMask: "emailAddresses,names",
};
const res = People.People.batchCreateContacts(obj);
console.log(res);
Allows up to 200 contacts in a single request.
3. batchDeleteContacts
// Please set this object for deleting data.
const obj = { resourceNames: ["people/###", , ,] };
const res = People.People.batchDeleteContacts(obj);
console.log(res);
It’s repeatable. Allows up to 500 resource names in a single request.
- In this case, the empty value is returned.
4. batchUpdateContacts
In order to use batchUpdateContacts
, it seems that it is required to include etag
. Please be careful about this. In this sample script, the latest etags are retrieved using getBatchGet
.
// Please set this object for updating data.
const obj = [
{
resourceName: "people/###",
update: { nicknames: [{ value: "sample nickname" }] },
},
,
,
,
];
// Create options and retrieve etags.
const { opt1, opt2 } = obj.reduce(
(o, { resourceName, update }) => {
o.opt1.resourceNames.push(resourceName);
Object.keys(update).forEach((f) => {
if (!o.opt1.personFields.includes(f)) {
o.opt1.personFields.push(f);
o.opt2.updateMask.push(f);
o.opt2.readMask.push(f);
}
});
return o;
},
{
opt1: { resourceNames: [], personFields: [] },
opt2: { updateMask: [], readMask: [] },
}
);
const { responses } = People.People.getBatchGet(opt1);
// Update using the inputted object.
Object.entries(opt2).forEach(([k, v]) => (opt2[k] = v.join(",")));
const contacts = obj.reduce(
(o, { resourceName, update }, i) => (
(o[resourceName] = { ...update, etag: responses[i].person.etag }), o
),
{}
);
const res = People.People.batchUpdateContacts({ contacts, ...opt2 });
console.log(res);
Allows up to 200 contacts in a single request.
References
- Method: people.getBatchGet
- Method: people.batchCreateContacts
- Method: people.batchDeleteContacts
- Method: people.batchUpdateContacts