> ## Documentation Index
> Fetch the complete documentation index at: https://www.ayrshare.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Airtable

> 從 Airtable 管理使用者的社群媒體帳號

## 概觀

[Airtable](https://www.airtable.com) 是一個功能多元的雲端平台，將試算表的結構與資料庫的強大能力無縫融合。

透過將 Airtable 的自動化指令碼（以 JavaScript 撰寫）與 Ayrshare 的 API 整合，你可以直接在 Airtable 內簡化社群媒體管理。此整合讓你能夠代表使用者發布內容、分析成效指標、管理留言——全部集中於一處。

在本指南中，我們將帶你逐步完成整合設定。

## 設定

<Warning>
  執行 Airtable 自動化指令碼需要 [付費](https://airtable.com/pricing) 的 Airtable 方案。
  請確認你的 Airtable 方案包含含指令碼的自動化功能。
</Warning>

本指南示範如何透過 [Ayrshare](https://www.ayrshare.com) 從 Airtable 自動發布到已連結的社群媒體帳號。
你可以發布到單一公司的社群帳號，或發布到你所管理客戶的社群媒體帳號。以下所有程式碼都可以在 [GitHub](https://github.com/ayrshare/airtable-post-social-media/blob/main/script.js) 上找到。

<Card horizontal icon="github" href="https://github.com/ayrshare/airtable-post-social-media/blob/main/script.js" title="GitHub：Airtable Post Social Media" />

## 取得你的 API Key

首先在 [Ayrshare 儀表板](https://www.ayrshare.com) 取得免費或付費方案的 API Key。此金鑰將用於下方的指令碼。

如果你使用 Ayrshare [Launch 方案](/multiple-users/business-launch-overview) 或 [Business 方案](https://www.ayrshare.com/business-plan-for-multiple-users/)，並希望代表客戶發文，請透過 /user 或 /create-profile 端點，或從 Ayrshare 儀表板取得所有客戶的 Profile Key。

<Tip>
  請確認你已在 Ayrshare 連結一些社群帳號。你可以在 [Ayrshare 社群連結頁面](https://app.ayrshare.com/social-accounts) 進行確認。
</Tip>

## 建立 Airtable Workspace

在 [Airtable](https://www.airtable.com) 中建立新的 workspace 並加入以下欄位。請務必依照下列名稱命名欄位：

<ul class="custom-bullets">
  <li>`Post` 設為 Long Text</li>

  <li>
    `Platforms` 設為 Multi Select，選項包含：facebook、instagram、twitter、linkedin、reddit 與 telegram
  </li>

  <li>`Images` 設為 Attachment</li>
  <li>`Profile Keys` 設為 Single Line Text</li>
  <li>`Status` 設為 Single Line Text。</li>
  <li>`Schedule Date` 設為 Date，使用 Local Format、Include Time Field、Time Format 24 Hours</li>
</ul>

<img class="center" src="https://mintcdn.com/ayrshare-docs/6mPR4kmwNEz1kMbX/images/packages-guides/airtable-example.webp?fit=max&auto=format&n=6mPR4kmwNEz1kMbX&q=85&s=ac8b91e08f67f7c2432ddfbbf7e65aa0" alt="Airtable Table Example" width="1536" height="161" data-path="images/packages-guides/airtable-example.webp" />

這些欄位將用於我們接下來要建立的 Airtable 自動化指令碼。

參見 [實際 Airtable 範例](https://airtable.com/shrCWY0oA1ghB42tI/tblpnTEiPwyuViqBo)。

<Card horizontal icon="book-open" href="https://airtable.com/shrCWY0oA1ghB42tI/tblpnTEiPwyuViqBo" title="Airtable Social Media Example">
  參見實際 Airtable 範例
</Card>

## 輸入測試貼文資料

我們需要一些範例資料來測試發文。以下是建議內容：

<ul class="custom-bullets">
  <li>`Post`：輸入 *Happy New Year 2025* </li>

  <li>
    `Platforms`：選擇一個或多個你已連結的網路。請確認名稱為小寫。
  </li>

  <li>
    `Images`：附加一張圖片。我們推薦這張，你可以下載後附加：
    [https://img.ayrshare.com/012/gb.jpg](https://img.ayrshare.com/012/gb.jpg)
  </li>

  <li>
    `Profile Keys`：如果你使用 Business 方案或 Launch 方案並希望發布到客戶的設定檔，請輸入
    他們的 Profile Key。*否則請留空。*
  </li>

  <li>
    `Status`：輸入 *pending*。指令碼只會擷取狀態設為 pending 的記錄。請確認
    「`pending`」為小寫。
  </li>

  <li>
    `Schedule Date`：目前先留空，因為我們只會測試立即發文。之後你可以
    選擇未來日期以排程發文。
  </li>
</ul>

## 使用指令碼編輯器建立自動化指令碼

接下來我們將建立 Airtable 自動化指令碼，從你的資料表讀取資料、建立貼文，並透過 Ayrshare 送到社群網路。你會使用 Airtable 的指令碼編輯器。

### 新增觸發器

* 在 workspace 中，點選 *Automation*，然後點選 *+New automation.*

<img class="center" src="https://mintcdn.com/ayrshare-docs/6mPR4kmwNEz1kMbX/images/packages-guides/new%20automation.webp?fit=max&auto=format&n=6mPR4kmwNEz1kMbX&q=85&s=d2637323e3e540d6e46cf0d45e2f161a" alt="Go to the Automation section of Airtable" width="932" height="144" data-path="images/packages-guides/new automation.webp" />

<ul class="custom-bullets">
  <li>為此自動化命名。</li>
  <li>點選 *Choose a Trigger*。</li>
  <li>選擇 When a Record is Created。</li>
  <li>選擇具有上述欄位的資料表。</li>
  <li>點選 Done。</li>
</ul>

## 新增動作

<ul class="custom-bullets">
  <li>先點選 *Add Action*。</li>
  <li>選擇 *Run Script*。你會進入指令碼編輯器。</li>
</ul>

**刪除** 這一行：

```javascript theme={"system"}
console.log(`Hello, ${base.name}!`);
```

並將下列程式碼 **複製並貼上** 到指令碼編輯器：

```javascript theme={"system"}
const API_KEY = "Your API Key"; // Get a free key at app.ayrshare.com

console.log(`Starting Post ${base.name}!`);

const sendPost = async (data) => {
  const { post, platforms, imageUrls, profileKeys, scheduleDate, shortenLinks } = data;

  const body = Object.assign(
    {},
    post && { post },
    platforms && { platforms },
    profileKeys && { profileKeys: profileKeys.split(",") },
    Array.isArray(imageUrls) &&
      imageUrls.length > 0 && {
        mediaUrls: imageUrls.map((image) => image.url)
      },
    scheduleDate && { scheduleDate },
    shortenLinks !== undefined && shortenLinks !== null && { shortenLinks }
  );

  console.log("Posting JSON:", JSON.stringify(body, null, 2));

  if (profileKeys) {
    body.profileKeys = profileKeys.split(",");
  }

  const response = await fetch("https://api.ayrshare.com/api/post", {
    method: "POST",
    body: JSON.stringify(body),
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${API_KEY}`
    }
  }).then((res) => res.json());

  return response;
};

const table = base.getTable("Posts");
const query = await table.selectRecordsAsync();
const filteredRecords = query.records.filter((record) => {
  const status = record.getCellValue("Status");
  return status === "pending";
});

for (let record of filteredRecords) {
  const post = record.getCellValue("Post");
  const images = record.getCellValue("Images");
  const platforms = record.getCellValue("Platforms");
  const profileKeys = record.getCellValue("Profile Keys");
  const scheduleDate = record.getCellValue("Schedule Date");

  const shortenLinks = false;
  const response = await sendPost({
    post,
    platforms: platforms.map((x) => x.name),
    imageUrls: images,
    profileKeys,
    scheduleDate,
    shortenLinks
  });

  console.log(response);

  if (response) {
    let status;
    if (Array.isArray(response)) {
      status = response.map((x) => x.status).every((x) => x === "success") ? "success" : "error";
    } else {
      status = response.status;
    }

    await table.updateRecordAsync(record, {
      Status: status
    });
  }
}
```

此程式碼會從你的資料表讀取並發布到 Ayrshare。不過，你必須先加入你的 API Key（如上取得）。

將 `Your API Key` 替換為你真實的 API key。

## 測試指令碼

在指令碼編輯器中，按下 *>Test*

指令碼會執行並輸出 API 呼叫的回應。如果一切運作正常，你會看到成功訊息回傳、記錄中的 pending 欄位變為 success，以及在所選社群網路上出現的貼文。

之後只要在資料表中建立新記錄，指令碼就會執行並處理狀態為 pending 的記錄。

## Airtable 文件

如需更多資訊，請參閱 [Airtable 文件](https://www.airtable.com/developers)。
其中有如何使用 Airtable 指令碼編輯器的詳細說明。

## 影片教學

以下是 Automate All The Things 團隊所製作的優質影片教學。該影片一步步示範如何將 Ayrshare 整合到實際的 Airtable 專案中。

<div class="video-container">
  <iframe width="560" height="315" src="https://www.youtube.com/embed/U_jo6f5hsyY" title="Post To Social Media From Airtable" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" />
</div>

更多 [將 Airtable 與社群媒體整合的範例](https://www.ayrshare.com/blog/automatically-post-to-social-media-from-airtable/)。

## 問題

如果你有任何問題或意見，請透過 [電子郵件](mailto:contact@ayrshare.com) 或 <a href="javascript:void(0)" onClick={() => window.Intercom && window.Intercom('show')}>與我們洽談</a> 聯絡我們。
