0

I have superadmin access to a domain, and I would like to create calendar resources using Google Apps Script.

Normally we can create calendar resources in Google admin console with admin access (https://accounts.google.com)...But but I want to automate it. I checked this page (https://developers.google.com/admin-sdk/directory/v1/reference/resources/calendars/insert).. and adapted it to a script, below, but I'm getting a "Bad Request" error.

What is wrong here?

This is my code--

function myFunction() {
  var cus = 'email id';
  var res ={
    resource : 'Name of the resource',
    resourceDescription : "Meeting Room",
    resourceType : "Room"
  }
 AdminDirectory.Resources.Calendars.insert(res, cus); 
}`
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Kanchan
  • 65
  • 2
  • 11

3 Answers3

1

resourceId and resourceName are mandatory fields. resourceDescription and resourceType are optional. You have 'resource' instead of resourceName and you haven't provided an Id.

See the purple section on this page https://developers.google.com/admin-sdk/directory/v1/reference/resources/calendars/insert

Serge Hendrickx
  • 1,416
  • 9
  • 15
  • How can I get this resource id ?? In admin console , We get the resource id, once the resource is created. – Kanchan Sep 19 '16 at 08:38
1

I have added to Kanchan's code few more lines to allow Google spreadsheet as a source file for resources to upload.

Manual:

  1. Replace "id of the sourcefile" - with actual ID of spreadsheet you are going to use

  2. The script will not read from the first row of the sheet. You can use it for headings. The columns sequence I have used was: ResourceName, ResourceID and then ResourceType. Example file is here: https://docs.google.com/spreadsheets/d/110UuIxsdJKWmff1RgMphcycbGOo_Ggrr1ON6PPj0tB4/

  3. It's best to create resources using Google resourceName naming convention: https://support.google.com/a/answer/1033941

  4. ResourceId is a string you need to define yourself. I haven't tried loading two resources with the same ID - I don't know what would happen if you did.

And now the code block:

function readwriteResources() {

  var cus = 'my_customer';
  var sourcefileid = "id of the sourcefile";
  var spreadsheet = SpreadsheetApp.openById(sourcefileid);
  var dataset = spreadsheet.getSheetByName('resources to load').getDataRange().getValues();

  for (i=1; i < dataset.length; i++) {
    var res = {
      resourceName : dataset[i][0],
      resourceId : dataset[i][1],
      resourceType : dataset[i][2]
    }

     AdminDirectory.Resources.Calendars.insert(res, cus);    
  }     
}
Radek
  • 56
  • 3
0

you don't get an ID when creating new resources through Apps Script, you provide one yourself.

Radek
  • 56
  • 3
  • Yes..I was under the impression that resource id will be created once the resource is created and not the other way.. Thanks for the help anyway.. – Kanchan Oct 16 '16 at 05:29