Google App Maker如何从Google通讯录创建数据源

时间:2018-01-16 18:49:46

标签: google-app-maker

使用GoogleAppMaker如何从谷歌联系人创建数据源。有一个员工HR示例应用程序,但我想类似地管理联系人(添加,修改,删除)和使用选择标准。

1 个答案:

答案 0 :(得分:6)

此时此任务在App Maker中并不简单,而且非常通用。我们可以将问题措辞更改为CRUD operations with 3rd party datasources。让我们把它分成更小的部分并分别解决它们。

阅读/列出联系人

这项任务相对容易。您需要使用Calculated Model代理Apps Scripts Contacts API响应。使用Contact响应中的字段子集创建模型后,您可以为模型创建数据源并将其绑定到List或Table小部件。您还可以尝试在Calculated Model Sample中找到一些灵感。

Fields

Datasource

// Server side script
function getContacts_() {
  var contacts = ContactsApp.getContacts();

  var records = contacts.map(function(contact) {
    var record = app.models.Contact.newRecord();

    record.FirstName = contact.getGivenName();
    record.LastName = contact.getFamilyName();

    var companies = contact.getCompanies();

    if (companies.length > 0) {
      var company = companies[0];

      record.Organization = company.getCompanyName();
      record.Title = company.getJobTitle();
    }

    var emails = contact.getEmails();

    if (emails.length > 0) {
      record.Email = emails[0].getAddress();
    }

    var phones = contact.getPhones();

    if (phones.length > 0) {
      record.Phone = phones[0].getPhoneNumber();
    }

    return record;
  });

  return records;
}

创建/更新/删除

由于计算模型有一些limitations,我们需要打开我们的想象力来创建,更新和删除其数据源中的记录。响应用户端的用户操作, CUD 操作的基本策略为calling server side scripts。要从UI获取用户输入,我们需要使用页面的Custom Properties,每个Contact字段都有一个属性: Custom properties

以下是一些可以解释这个想法的片段

创建

// Client script
function onSubmitContactClick(submitButton) {
  var props = submitButton.root.properties;

  var contact = {
    FirstName: props.FirstName,
    LastName: props.LastName,
    Organization: props.Organization,
    ...
  };

  google.script.run
    .withSuccessHandler(function() {
      // Most likely we'll need to navigate user back to the
      // page with contacts list and reload its datasource
      // to reflect recent changes, because our `CUD` operations
      // are fully detached from the list datasource
      app.showPage(app.pages.Contacts);
      app.datasources.Contacts.load();
     })
    .withFailureHandler(function() {
       // TODO: Handle error
     })
    .createContact(contact);
}


// Server script
function createContact(contactDraft) {
 var contact = ContactsApp.createContact(contactDraft.FirsName,
                                         contactDraft.LastName,
                                         contactDraft.Email);

  contact.addCompany(contactDraft.Organization, contactDraft.Title);
  contact.addPhone(ContactsApp.Field.WORK_PHONE, contactDraft.Phone);
}

<强>更新

更新联系人记录的想法与新的联系人创建流程非常相似,所以我暂时跳过它。

删除

假设删除按钮位于联系人表格行内。

// Client script
function onDeleteContactClick(deleteButton) {
  var email = deleteButton.datasource.item.Email;

  google.script.run
    .withSuccessHandler(function() {
      // To update contacts list we can either reload the entire
      // datasource or explicitly remove deleted item on the client.
      // Second option will work way faster.
      var contactIndex = deleteButton.parent.childIndex;
      app.datasources.Contacts.items.splice(contactIndex, 1);
     })
    .withFailureHandler(function() {
      // TODO: Handle error
     })
    .deleteContact(contact);
}


// Server script
function deleteContact(email) {
  var contact = ContactsApp.getContact(email);
  ContactsApp.deleteContact(contact);
}