65.9K
CodeProject 正在变化。 阅读更多。
Home

如何将 Google 表单响应捕获到 Kintone 数据库中

2019 年 6 月 6 日

CPOL
viewsIcon

3880

本文介绍了如何将 Google 表单回复捕获到 Kintone 数据库,而不是将它们捕获到电子表格中。

Screenshot of the integration image between Google Forms and Kintone.

引言

本文介绍了如何将 Google 表单回复捕获到 Kintone 数据库,而不是将它们捕获到电子表格中。

Google 表单允许最终用户轻松创建和发送问卷和活动邀请,并将结果存储到电子表格中。然而,将表单回复捕获到 Kintone 数据库(又称 Kintone 应用)中具有许多优势:

  1. 可以设置回复的访问控制
    Kintone 允许最终用户对其应用中的数据设置精细的访问控制。例如,可以设置访问控制,以便只有人力资源部门的成员才能查看提交的电子邮件地址。

    GIF of how the access controls can hide sensitive information such as email addresses from other Kintone users.

  2. 可以为每个回复定义业务流程管理
    可以为 Kintone 应用中的每条记录设置工作流,这使得收到回复后需要采取的操作变得更加清晰。例如,在将 Google 表单回复接收到 Kintone 后,可以将该记录分配给设计团队成员,以处理与回复数据相关的创意设计。

    GIF of proceeding the status so that a user is assigned a task.

  3. 与团队成员的沟通变得简单
    Kintone 应用中的每条记录都具有发布评论的功能,供其他团队成员阅读。例如,在收到 Google 表单回复后,活动团队成员可以在记录中写评论,以联系可能与表单提交者有联系的销售团队成员。

    GIF of a user posting a comment to a user, so that they will be notified and be able to read the details of the submission.

在本文中,我们将通过以下 4 个步骤完成集成

  1. 准备 Google 表单
  2. 准备 Kintone 应用
  3. 创建 Google Apps Script 程序
  4. 测试集成

1. 准备 Google 表单

本节介绍如何使用 Google 表单构建活动邀请表单的步骤。

步骤 1

此步骤需要一个 Google 帐户。
登录您的 Google 帐户,从 Google 应用图标中选择 Google 表单(或直接从https://docs.google.com/forms/登录),然后创建一个新的空白表单。
 

Screenshot of the Google Forms home page after logging in.

第二步

输入表单的标题和描述。

Screenshot of a new form where the title and description are entered in.

步骤 3

单击“设置”图标,勾选“收集电子邮件地址”,取消勾选“需要登录”下的所有复选框,然后保存更改。

Screenshot of the option in Google Forms to collect email addresses from the form submitters.

步骤 4

通过单击“添加问题”图标并选择问题类型(例如“多项选择”)来设置活动邀请表单的内容。在问题字段中输入您的问题,然后输入回复选项。

Screenshot of placing various question types into the Google form.

如果问题是必填项,请如下选择“必填”。

Screenshot of making certain questions mandatory in Google Forms.

步骤 5

重复此过程,直到活动邀请表单完成。

Screenshot of adding in more questions in to Google Forms.

例如,以下是示例活动邀请表单中使用的表单详细信息

问题类型 提问 选项
多项选择 您想参加此活动吗?

可能
简答 参与者人数  
长答 请填写参与者的姓名  

Google 表单将保存您所做的每一个更改。完成表单后,继续下一步——准备 Kintone 应用。

2. 准备 Kintone 应用

本节介绍如何在您的 Kintone 环境中设置 Kintone 应用实例,该实例将捕获 Google 表单回复。如果您没有 Kintone 环境,可以通过加入Kintone 开发者计划申请一年的免费 Kintone 开发者许可证。

步骤 1

登录您的 Kintone 环境,导航到门户并在“应用”小部件中单击 [+] 按钮创建新的 Kintone 应用。选择“从头开始创建应用”,并使用与 Google 表单中类似的字段设置 Kintone 表单。

GIF of placing in a field within a Kintone App and editing the settings.

作为参考,这是本文中使用的 Kintone 应用表单布局和设置。

Screenshot of fields placed into the Kintone App similar to the questions placed into the Google Forms invitation.

字段类型 字段名 字段代码 其他设置
链接 电子邮件 电子邮件 类型:电子邮件地址
单选按钮 您想参加此活动吗? 参加 选项


可能
数字 参与者人数 参与者人数 -
文本区 参与者姓名 参与者姓名 -

在表单中布置好字段后,单击“保存”。

第二步

单击“应用设置”选项卡,然后单击“API 令牌”。

Screenshot of showing where the API Token option is inside the Kintone App settings.

为应用生成一个新的 API 令牌,勾选“添加记录”复选框,然后保存设置。

Screenshot of generating a new API Token for the Kintone App and selecting the Add records permission.

单击“更新应用”以应用对您的应用所做的更新

3. 创建 Google Apps Script 程序

本节介绍如何设置 Google Apps Script 程序,以便在 Google 表单提交后运行,从而将回复记录到 Kintone 应用中。

步骤 1

重新打开上一步中创建的 Google 表单,然后从“其他”菜单中单击“脚本编辑器”。
 

Screenshot of selecting the Script editor option for the Event Invitation form created in Google Forms.

输入项目名称和文件名。
 

Screenshot of entering a Project name and File name into the Google script editor.

第二步

在本文中,我们将使用以下库:https://github.com/Arahabica/KintoneManager

从上面的链接复制项目密钥。在 Google 表单上,选择“资源”,然后选择“”。
 

Screenshot of selecting a library to use with the Google script editor.

将项目密钥粘贴到“添加库”字段中。单击“添加”以添加库,选择最新版本,然后保存设置。

Screenshot of adding in the project key of the library and selecting the version of the library.

步骤 3

将以下代码复制到您的 Google Apps Script 中。
代码中列出的字符串“Email”、“attend”、“number_of_participants”和“names_of_participants”是指 Kintone 应用中字段的字段代码。请确保这些字符串与您应用表单中字段的字段代码匹配。

更新代码,使“subdomain”、“appid”和“token”键的值与您的 Kintone 环境和 Kintone 应用相对应(通过浏览器导航到应用以在 URL 中查找作为整数的应用 ID)。

function getFormResponse(e) {
    'use strict';
    var itemResponses = e.response.getItemResponses();
    var records = '[';
    
    records += Utilities.formatString('{"Email": { "value": "%s" }', e.response.getRespondentEmail());

    for (var i = 0; i < itemResponses.length; i++) {
        var itemResponse = itemResponses[i];

        switch (itemResponse.getItem().getTitle()) {
            case 'Would you like to participate in this event?':
                records += Utilities.formatString(',"attend" : { "value": "%s" }',
                    itemResponse.getResponse());
                break;
            case 'The number of participants':
                records += Utilities.formatString(',"number_of_participants" : { "value": "%s" }',
                    itemResponse.getResponse());
                break;
            case 'Please enter the names of the participants':
                records += Utilities.formatString(',"names_of_participants" : { "value": "%s" }',
                    itemResponse.getResponse());
                break;
        }
    }
    records += '}]';
    Logger.log('Response JSON is "%s"', records);
    return records;
}

function sendToKintone(e) {
    'use strict';
    Logger.log('Form submitted');
    var subdomain = '{subdomain}.kintone.com'; // change URL to your kintone domain 
    var apps = {
        YOUR_APPLICATION1: { appid: 1, name: 'Kintone Connect', token: 'xxxxxxxxx' }
    };
    var manager = new KintoneManager.KintoneManager(subdomain, apps);// Initialize library
    var str = getFormResponse(e);
    str = str.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
    var records = JSON.parse(str);// Convert to JSON
    var response = manager.create('YOUR_APPLICATION1', records); //Create a record in kintone
    // Status code 200 will return for successful requests
    var code = response.getResponseCode();
    Logger.log('Response code is "%s"', code);
}

完成后,保存您的代码。

代码解释:将数据发送到 Kintone

e.response.getItemResponses() 函数检索提交的表单数据

function getFormResponse(e) {
    var itemResponses = e.response.getItemResponses();
    // ***
    // ***
}

e.response.getRespondentEmail() 函数用于获取表单提交者的电子邮件地址,并创建 Kintone 的请求数据

var records = '[';
    
    records += Utilities.formatString('{"Email": { "value": "%s" }', e.response.getRespondentEmail());

    for (var i = 0; i < itemResponses.length; i++) {
        var itemResponse = itemResponses[i];

        switch (itemResponse.getItem().getTitle()) {
            case 'Would you like to participate in this event?':
                records += Utilities.formatString(',"attend" : { "value": "%s" }',
                    itemResponse.getResponse());
                break;
            case 'The number of participants':
                records += Utilities.formatString(',"number_of_participants" : { "value": "%s" }',
                    itemResponse.getResponse());
                break;
            case 'Please enter the names of the participants':
                records += Utilities.formatString(',"names_of_participants" : { "value": "%s" }',
                    itemResponse.getResponse());
                break;
        }
    }
records += '}]';

代码解释:将数据发送到 Kintone

上面创建的 Kintone 应用的信息设置如下:

function sendToKintone(e) {
    var subdomain = '{subdomain}.kintone.com'; // change URL to your kintone domain 
    var apps = {
        YOUR_APPLICATION1: { appid: 1, name: 'Kintone Connect', token: 'xxxxxxxxx' }
    };
    // ***
    // ***
}

导入的库被初始化,请求数据被格式化为 JSON,然后发送到 Kintone

var manager = new KintoneManagerUs(subdomain, apps);// Initialize library
var str = getFormResponse(e);
str = str.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");
var records = JSON.parse(str);// Convert to JSON
var response = manager.create('YOUR_APPLICATION1', records); //Create a record in kintone

如果响应代码为“200”,则数据发布到 Kintone 成功。

var code = response.getResponseCode();

步骤 4

从“编辑”菜单中选择“当前项目的触发器”,然后选择一个函数,以便在 Google 表单提交时运行。完成后单击“保存”。

Screenshot of selecting the Current project’s triggers in the Google Script editor.

Screenshot of selecting what action will trigger the code to run in the Google Script editor.

Google Apps Script 的设置现已完成。

4. 测试集成

本节介绍如何测试新设置的集成。

单击您创建的 Google 表单右上角的“发送”按钮,填写“发送”表单,然后单击“发送”按钮。

Screenshot of sending out the Event invitation form to recipients via Google Forms.

收件人将收到 Google 表单的链接。

Screenshot of filling out the Event invitation form.

提交表单后,回复数据应添加到您的 Kintone 应用中。

Screenshot of a record inside a Kintone App with the details that were entered in to the Event invitation form of Google Forms.

5. 总结

使用 Google 表单,您可以轻松创建问卷和活动邀请表单,并通过电子邮件将其发送给收件人,或将其嵌入到您的公司网站上。通过在 Kintone 中管理回复,您可以利用 Kintone 的功能来设置访问控制、定义业务流程管理,并轻松地为每个回复留下供其他团队成员阅读的笔记。

有关如何使用 Kintone 数据库及其 API 的更多详细信息,请通过以下链接尝试 5 分钟的 Kintone API 教程。该教程还为您提供了一个免费的 Kintone 开发者环境。祝您使用愉快!

A link to start a 5-minute Kintone API Tutorial on the Kintone Developer Program website.

© . All rights reserved.