0

在查看 AirTable API 时,似乎没有允许以编程方式 (API) 插入 CSV 以在基础中创建新选项卡的功能。

4

1 回答 1

0

Airtable 的 API 允许您在给定的 Airtable 库中创建、读取、更新和删除记录。您可以遍历 CSV,将每一行转换为与 Airtable API 匹配的格式,然后在 Airtable 中创建新记录。

Airtable 提供了一个 JavaScript SDK,因此我将使用 JS 来编写一些示例代码(但您可以用任何语言重复此设置)。

const Airtable = require("Airtable");
const fs = require('fs');
const csv = require('fast-csv');

// create the info we need to connect to Airtable
var base_id = "YOUR_BASE_ID";
var base = Airtable.base(base_id);
var table_name = "YOUR_TABLE_NAME";
var table = base.table(table_name);

// file we want to upload
var filename = 'YOUR_FILE_NAME.csv';

// read all rows from your CSV
// I generally like the package "fast-csv" for help with this
async function read_csv(filename) {
  var p = new Promise((resolve, reject) => {
    let rows = []
    fs.createReadStream(filename)
      .pipe(csv.parse({
        headers: true
      }))
      .on('data', row => {
        rows.push(row);
      })
      .on('end', () => {
        resolve(rows);
      })
      .on('error', (err) => {
        reject(err);
      })
  });
  var rows = await Promise.resolve(p);
  return rows;
}

// read the CSV file, and then push the rows into Airtable
read_csv(filename)
  .then(async (rows) => {
    // The Airtable API allows you to batch 10 records together at a time
    // so we can take chunks of ten rows, format each one, package and send
    let i, j, chunk;
    let size = 10;
    for (i = 0, j = rows.length; i < j; i += size) {
      chunk = rows.slice(i, i + size);

      // format each record in our chunk to match 
      // in this example, we are taking two fields from our CSV
      // and creating new records using those
      // You should change the field names here to map to your CSV and Airtable Base
      let payload = chunk.map((r) => {
        return {
          'fields': {
            'Project ID': r['Project ID'],
            'Employee Name': r['Employee Name']
          }
        }
      });

      // make the request
      try {
        let result = await table.create(payload);
      } catch (err) {
        throw err;
      }
    }

    // log all complete
    console.log("All records created");
  })
  .catch((err) => {
    console.log("Error: ", err);
  })
于 2019-12-28T19:43:22.637 回答