如何从 R 进行查询并发布到 Monday.com 的看板? (有答案)

问题描述 投票:0回答:1

我有这个问题,但我在这里找不到解决方案。所以我决定分享我发现的基础知识。

r monday.com
1个回答
0
投票

我假设您已经拥有令牌。

首先,您需要 httr 和 jsonlite 来处理查询和帖子以及 json 响应。

An example board from Monday.com with emphasis in the board id

然后,从您感兴趣的版块中,例如上图中的版块,您需要 id。


然后我们需要每列(除了 item 列)的 id,我们使用以下查询

query <- '{
boards(ids: [7417519601]) {
name
columns {
  title
  id
  type
}
groups {
  title
  id
 }
}
}'

然后我们发送 POST(我假设响应成功)

response <- POST(
  url = "https://api.monday.com/v2",
  add_headers(Authorization = apiKey),
  body = list(query = query),
  encode = "json"
)

转换响应后,我们检查列类型及其 ID

data <- content(response, as = "text")
data_json <- fromJSON(data, flatten = TRUE)
data_json$data$boards$columns |> as.data.frame() |> View()

View of the column types and id

检查 API Playground 中的查询很有用。


我们还需要组id,可以通过查询找到

query <- '
{
  boards (ids: 7417519601) {
    groups {
      id
      title
    }
  }
}
'

在与之前相同的响应代码和 json 处理之后,我们检查组 id

data_json$data$boards$groups |> as.data.frame() |> View()

id from existing groups in the board

我们的板块名为“Principal”,但它的id是“topics”


现在我们将发布一个项目。有些类型最初很棘手,因此在 API Playground 中检查现有项目(如果有人需要,我可以稍后添加示例)也很有帮助。

为此,我们需要一个突变

mutation <- sprintf('
mutation {
  create_item (
    board_id: 7417519601,
    group_id: "topics", 
    item_name: "An item 1",   #can be sent as parameter too
    column_values: \"{
    \\\"text__1\\\":\\\"%s\\\", 
    \\\"numbers__1\\\":\\\"%d\\\",
    \\\"date4\\\":\\\"%s\\\",
    \\\"email__1\\\": {\\\"text\\\": \\\"%s\\\" , \\\"email\\\": \\\"%s\\\" },
    \\\"status\\\":{\\\"index\\\":%s}
    }\") 
   {
    id
    name
  }
}
', "Neo", 42, "2024-09-11", "[email protected]", "[email protected]", 0
)

及其帖子

response <- POST(
  url = "https://api.monday.com/v2",
  add_headers(Authorization = apiKey),
  body = list(query = mutation),
  encode = "json"
)

我们就有了 successful post from R

  • 我们可以只发送部分值。在这种情况下,项目中将显示为空
  • 电子邮件需要两个参数:实际电子邮件及其在项目行中的显示方式的文本。这里我发送了两次电子邮件,
  • 日期应为“YYYY-MM-DD”
  • 有时数字也可以以%s形式发送,只要是数字格式(数字或字符变量),列类型就会按原样处理;有时你会被迫使用 %s 因为 R 会告诉你......
  • 状态。这里我们使用了 {\"index\":%s} 和组中显示为“Dev”的 0 参数。我们也可以使用 {\"label\":\"%s\"} 和 "Dev" 作为参数

如果查询有拼写错误或其他问题,响应会成功,但不会到达群组。


也许这太基础了,但我想有人可以用它作为开始,即使我们周围有 chat-gpt。

© www.soinside.com 2019 - 2024. All rights reserved.