Google App Maker如何从Google通讯录创建数据源 [英] Google App Maker how to create Data Source from Google Contacts

查看:73
本文介绍了Google App Maker如何从Google通讯录创建数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用GoogleAppMaker如何从Google联系人创建数据源.有一个员工人力资源示例应用程序,但我想同样地管理联系人(添加,修改,删除)并使用选择条件.

Using GoogleAppMaker how to create a data source from google contacts. There is an employee HR example app but I want to similarly manage contacts (add, modify, delete) and use select criteria.

推荐答案

目前,此任务在App Maker中并不简单,并且非常通用.我们可以将问题措辞更改为CRUD operations with 3rd party datasources.让我们将其分成较小的部分,然后分别解决.

At this time this task is not trivial in App Maker and it is pretty much generic. We can change question wording to CRUD operations with 3rd party datasources. Let's break it into smaller parts and address them separately.

阅读/列出联系人

此任务相对容易.您需要使用计算模型代理 Contact 响应中的字段子集创建模型后,即可创建模型的数据源,并将其绑定到列表"或表"窗口小部件.您还可以尝试在计算出的模型样本中找到灵感.

This task is relatively easy. You need to use Calculated Model to proxy Apps Scripts Contacts API response. Once you create model with subset of fields from the Contact response you can create datasource for the model and bind it to List or Table widget. You can also try to find some inspiration in Calculated Model Sample.

// 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;
}

创建/更新/删除

由于计算模型具有某些限制,因此我们需要发挥想象力从其数据源创建,更新和删除记录.基本策略是调用服务器端脚本 CUD 操作,以响应客户端上的用户操作.为了从用户界面中获取用户的输入,我们将需要利用页面的自定义属性,即一个属性对于每个联系人"字段:

Since Calculated Models have some limitations, we need to turn on our imagination to create, update and delete records from their datasources. The basic strategy will be calling server side scripts for CUD operations in response to user actions on client side. To get user's input from UI we will need to utilize page's Custom Properties, one property for each Contact field:

这里有一些片段可以解释这个想法

Here are some snippets that should explain the idea

创建

// 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);
}

更新

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

Idea to update contact records will be very similar to the new contact creation flow, so I skip it for now.

删除

假定删除按钮位于通讯录表行内.

Assuming that delete button is located inside contacts table row.

// 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);
}

这篇关于Google App Maker如何从Google通讯录创建数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆