更新记录时的表格操作技巧

aki发表于:2016年10月07日 14:44:03更新于:2022年07月13日 15:05:33

kintone应用的记录中,可使用表格添加多行数据。类似报价、每月考勤数据、客户访问记录等使用表格的情况比较多。因此,本次向大家介绍如何使用kintone REST API对表格进行操作。
使用kintone REST API对记录内的表格进行更新时,有几点注意事项。

  • 更新记录时,如果不包含表格内的数据,则表格内的数据保持原样

  • 更新记录时,如果包含表格内的数据,不包含在请求中的数据不会被更新

综合以上注意事项,给大家介绍各种情况下的使用方法。

首先,准备记录

事先添加一条记录,记录中包含文本内容和2行表格。
kintone API需要用到字段代码,为了方便起见,字段名称和字段代码设成一样。

00157f744818582ad26d748da33e705

获取记录的值

使用以下JSON格式可获取记录的值。

{
  "字段代码": {
    "type": "RECORD_NUMBER", // 字段类型
    "value": "1" // 字段的值
  }
}

使用以下JSON格式可获取表格字段的值。

{
  "表格的字段代码": {
    "type": "SUBTABLE",
    "value": [ // 行的数据
      {
        "id": "48290", // 行的 ID
        "value": {
          "字段代码": {
            "type": "SINGLE_LINE_TEXT", // 表格字段的字段类型
            "value": "TableText1" // 表格内的字段的值
          }
        }
     }
    ]
  }
}

使用获取一条记录的API,获取刚创建的应用记录,并确认实际记录的内容。

GET https://{subdomain}.cybozu.com/k/v1/record.json?app=2364&id=1
※ 在app中指定应用ID,在id中指定记录ID。

{
    "record": {
        "记录编号": {
            "type": "RECORD_NUMBER", 
            "value": "1"
        }, 
        "Title": {
            "type": "SINGLE_LINE_TEXT", 
            "value": "Sample"
        }, 
        "$revision": {
            "type": "__REVISION__", 
            "value": "1"
        }, 
        "Table": {
            "type": "SUBTABLE", 
            "value": [
                {
                    "id": "48290", 
                    "value": {
                        "Text": {
                            "type": "SINGLE_LINE_TEXT", 
                            "value": "TableText1"
                        },
                        "Number": {
                            "type": "NUMBER"
                            "value": "1"
                        }
                    }
                }, 
                {
                    "id": "48291", 
                    "value": {
                        "Text": {
                            "type": "SINGLE_LINE_TEXT", 
                            "value": "TableText2"
                        },
                        "Number": {
                            "type": "NUMBER"
                            "value": "2"
                        }
                    }
                }
            ]
        }, 
        "创建时间": {
            "type": "CREATED_TIME", 
            "value": "2014-03-26T15:27:00Z"
        },
        ・・・
    }
}

更新记录表格

现在,使用更新一条记录的API来更新表格字段。

在表格末尾添加行

如要向表格添加行,请求数据中必须包含现有的行ID。
※ 如果无需更新现有行中的数据,则可省略现有行的值。

  • "app" ・・・要更新的应用的应用ID

  • "id" ・・・要更新的记录ID

  • "Table""value"内的数组中包含的"id" ・・・现有行ID

  • "Table""value"内的数组中包含的"value" ・・・要添加的行的值

{
    "app": 2364, 
    "id": 1, 
    "record": {
        "Table": {
            "value": [
                {
                    "id": "48290"
                }, 
                {
                    "id": "48291"
                }, 
                {
                    "value": {
                        "Text": {
                            "value": "要添加的文本"
                        },
                        "Number": {
                            "value": "3"
                        }
                    }
                }
            ]
        }
    }
}

省略现有行的ID,在请求数据中包含现有行的所有数据,也可以给表格添加值。
※实际上系统内部的操作是先删除所有行,再添加请求数据中包含的数据。

  • "app" ・・・要更新的应用的应用ID

  • "id" ・・・要更新的记录ID

  • "Table""value"内的数组中包含的"value" ・・・现有行的值+要添加的行的值

{
    "app": 2364, 
    "id": 1, 
    "record": {
        "Table": {
            "value": [
                {
                    "value": {
                        "Text": {
                            "value": "TableText1"
                        },
                        "Number": {
                            "value": "1"
                        }
                    }
                }, 
                {
                    "value": {
                        "Text": {
                            "value": "TableText2"
                        },
                        "Number": {
                            "value": "2"
                        }
                    }
                }, 
                {
                    "value": {
                        "Text": {
                            "value": "要添加的文本"
                        },
                        "Number": {
                            "value": "3"
                        }
                    }
                }
            ]
        }
    }
}

执行后的页面

00157f75b3add20841e729f8563cd4d

在表格第一行中插入一行

插入行的情况,也是跟在表格末尾添加行的处理一样。按照要在表格中显示的顺序,逐一添加到请求中。

  • "app" ・・・要更新的应用的应用ID

  • "id" ・・・要更新的记录ID

  • "Table""value"内的数组中包含的"id" ・・・现有行ID

  • "Table""value"内的数组中包含的"value" ・・・要添加的行的值

{
    "app": 2364, 
    "id": 1, 
    "record": {
        "Table": {
            "value": [
              {
                    "value": {
                        "Text": {
                            "value": "在第一行中插入"
                        },
                        "Number": {
                            "value": "0"
                        }
                    }
                },
                {
                    "id": "48290"
                }, 
                {
                    "id": "48291"
                }
            ]
        }
    }
}

这里也跟在表格末尾添加行时一样,可以省略现有行的ID,通过在请求中添加所有必要的数据来更新表格。

 {
    "app": 2364, 
    "id": 1, 
    "record": {
        "Table": {
            "value": [
                {
                    "value": {
                        "Text": {
                            "value": "在第1行中插入"
                        },
                        "Number": {
                            "value": "0"
                        }
                    }
                }, 
                {
                    "value": {
                        "Text": {
                            "value": "TableText1"
                        },
                        "Number": {
                            "value": "1"
                        }
                    }
                }, 
                {
                    "value": {
                        "Text": {
                            "value": "TableText2"
                        },
                        "Number": {
                            "value": "2"
                        }
                    }
                }
            ]
        }
    }
}

执行后的页面

00157f75bc294756a14ad9a431ab61f

只想更新表格中某行的特定字段

通过指定要更新的行的ID,可对该行的特定字段进行更新。
如同一行的其他字段无需更新,该字段可省略。
※ 省略现有行ID时,将删除相应的行。

  • "app" ・・・要更新的应用的应用ID

  • "id" ・・・要更新的记录ID

  • "Table"内的"id" ・・・要更新的行的ID

{
    "app": 2364, 
    "id": 1, 
    "record": {
        "Table": {
            "value": [
                {
                    "id": "48300",
                    "value": {
                        "Text": {
                            "value": "仅更新特定的字段"
                        }
                    }
                }, 
                {
                    "id": "48301",
                    "value": {
                        "Number": {
                            "value": "200"
                        }
                    }
                }
            ]
        }
    }
}

执行后的页面

00157f75c37218294548bda3b3069c2

更新了红色框内的部分。

注意事项

如果指定了不存在的行ID,则不会发出错误,而是其作为新的数据添加。

同时,将删除表中本来存在但未通过API指定行ID的数据。

补充)删除表格内的所有数据

Table对象内的value数组设置为空时,将删除表格中的数据。

  • "app" ・・・要更新的应用的应用ID

  • "id" ・・・要更新的记录ID

{
    "app": 2364, 
    "id": 1, 
    "record": {
        "Table": {
            "value": []
        }
    }
}

如果掌握了像上面这些表格的更新技术,可在很多情况下发挥作用。
请务必自己试试看。

该Tips在2014年4月版本中确认过。


回复(3)

  • 追梦人

    刚刚那个格式发错了,可以看看下面的这个,这个是我刚刚写的。

    (function() {
        "use strict";
        /**获取记录的值**/
        function getRecord(event) {
            //通过RestApi 的方式来获取数据
            //获取应用的id
            var appId = event.appId;
            var currentRecordId = kintone.app.record.getId();
            var params = {
                "app": appId,
                "id": currentRecordId
            };
            kintone.api(
                kintone.api.url("/k/v1/record", true),
                "GET",
                params,
                function(resp) {
                    debugger
                    alert("成功");
                    //查询成功以后获取到Table 中 要更新的行号
                    var tableRow = resp.record.Table.value;
                    if(tableRow.length > 0) {
                        var tableRowId = tableRow[1].id;
                        //进行更新操作
                        //在table 末尾添加一行记录
                        var params2 = {
                            //app:要更新的应用的id
                            "app": appId,
                            //id: 要更新的记录的id
                            "id": kintone.app.record.getId(),
                            "record": {
                                "Table": {
                                    "value": [
        {
                                            "value": {
                                                "Text": {
                                                    "value": "首行添加的文本"
                                                },
                                                "Number": {
                                                    "value": "4"
                                                }
                                            }
                                        },
                                        //只针对 table中行号为: 11262 的这一行的 Text 字段来进行更新
                                        {
                                            "id": tableRowId,
                                            "value": {
                                                "Text": {
                                                    "value": "按时来只针对这一个字段来进行更新"
                                                }
                                             //,
                                             //"Number": {
                                              //"value": "1"
                                             //}
                                            }
                                        },
                                        {
                                            "value": {
                                                "Text": {
                                                    "value": "TableText2"
                                                },
                                                "Number": {
                                                    "value": "2"
                                                }
                                            }
                                        },
                                        {
                                            "value": {
                                                "Text": {
                                                    "value": "添加的文本"
                                                },
                                                "Number": {
                                                    "value": "3"
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        };
                        kintone.api(
                            kintone.api.url("/k/v1/record", true),
                            'PUT',
                            params2,
                            function(resp) {
                                alert("更新成功")
                            },
                            function(resp) {
                                alert("更新失败,失败原因为: " + resp.message)
                            }
                        );
                    }
                },
                function(resp) {
                    alert("失败,错误原因为: " + resp.message);
                }
            );
        };
        kintone.events.on("app.record.detail.show", getRecord);
    })();

    引用 追梦人 的回复:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    (function() {
        "use strict";
        /**获取记录的值**/
        function getRecord(event) {
            //通过RestApi 的方式来获取数据
            //获取应用的id
            var appId = event.appId;
            var currentRecordId = kintone.app.record.getId();
            var params = {
                "app": appId,
                "id": currentRecordId
            };
            kintone.api(
                kintone.api.url("/k/v1/record"true),
                "GET",
                params,
                function(resp) {
                    debugger
                    alert("成功");
                    //查询成功以后获取到Table 中 要更新的行号
                    var tableRow = resp.record.Table.value;
                    if(tableRow.length > 0) {
                        var tableRowId = tableRow[1].id;
                        //进行更新操作
                        //在table 末尾添加一行记录
                        var params2 = {
                            //app:要更新的应用的id
                            "app": appId,
                            //id: 要更新的记录的id
                            "id": kintone.app.record.getId(),
                            "record": {
                                "Table": {
                                    "value": [
                                        {
                                            "value": {
                                                "Text": {
                                                    "value""首行添加的文本"
                                                },
                                                "Number": {
                                                    "value""4"
                                                }
                                            }
                                        },
                                        //只针对 table中行号为: 11262 的这一行的 Text 字段来进行更新
                                        {
                                            "id": tableRowId,
                                            "value": {
                                                "Text": {
                                                    "value""按时来只针对这一个字段来进行更新"
                                                },//
                                                "Number": {//
                                                    "value""1"//
                                                }
                                            }
                                        },
                                        {
                                            "value": {
                                                "Text": {
                                                    "value""TableText2"
                                                },
                                                "Number": {
                                                    "value""2"
                                                }
                                            }
                                        },
                                        {
                                            "value": {
                                                "Text": {
                                                    "value""添加的文本"
                                                },
                                                "Number": {
                                                    "value""3"
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        };
                        kintone.api(
                            kintone.api.url("/k/v1/record"true),
                            'PUT',
                            params2,
                            function(resp) {
                                alert("更新成功")
                            },
                            function(resp) {
                                alert("更新失败,失败原因为: " + resp.message)
                            }
                        );
                    }
                },
                function(resp) {
                    alert("失败,错误原因为: " + resp.message);
                }
            );
        };
        kintone.events.on("app.record.detail.show", getRecord);
    })();
  • 追梦人
    (function() {
        "use strict";
        /**获取记录的值**/
        function getRecord(event) {
            //通过RestApi 的方式来获取数据
            //获取应用的id
            var appId = event.appId;
            var currentRecordId = kintone.app.record.getId();
            var params = {
                "app": appId,
                "id": currentRecordId
            };
            kintone.api(
                kintone.api.url("/k/v1/record", true),
                "GET",
                params,
                function(resp) {
                    debugger
                    alert("成功");
                    //查询成功以后获取到Table 中 要更新的行号
                    var tableRow = resp.record.Table.value;
                    if(tableRow.length > 0) {
                        var tableRowId = tableRow[1].id;
                        //进行更新操作
                        //在table 末尾添加一行记录
                        var params2 = {
                            //app:要更新的应用的id
                            "app": appId,
                            //id: 要更新的记录的id
                            "id": kintone.app.record.getId(),
                            "record": {
                                "Table": {
                                    "value": [
                                        {
                                            "value": {
                                                "Text": {
                                                    "value": "首行添加的文本"
                                                },
                                                "Number": {
                                                    "value": "4"
                                                }
                                            }
                                        },
                                        //只针对 table中行号为: 11262 的这一行的 Text 字段来进行更新
                                        {
                                            "id": tableRowId,
                                            "value": {
                                                "Text": {
                                                    "value": "按时来只针对这一个字段来进行更新"
                                                },//
                                                "Number": {//
                                                    "value": "1"//
                                                }
                                            }
                                        },
                                        {
                                            "value": {
                                                "Text": {
                                                    "value": "TableText2"
                                                },
                                                "Number": {
                                                    "value": "2"
                                                }
                                            }
                                        },
                                        {
                                            "value": {
                                                "Text": {
                                                    "value": "添加的文本"
                                                },
                                                "Number": {
                                                    "value": "3"
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        };
                        kintone.api(
                            kintone.api.url("/k/v1/record", true),
                            'PUT',
                            params2,
                            function(resp) {
                                alert("更新成功")
                            },
                            function(resp) {
                                alert("更新失败,失败原因为: " + resp.message)
                            }
                        );
                    }
                },
                function(resp) {
                    alert("失败,错误原因为: " + resp.message);
                }
            );
        };
        kintone.events.on("app.record.detail.show", getRecord);
    })();

    引用 closer 的回复:

    这个表格更新字段有没有JavaScript的范例

  • closer

    这个表格更新字段有没有JavaScript的范例