如何用Google Apps Script批量更新Airtable的空记录(每批10条)
修改脚本实现批量更新Airtable已有空记录
核心修改逻辑
- Airtable更新记录必须指定目标记录的ID,所以第一步要先获取你提前创建的空记录ID
- 将Google Photos的相册数据与空记录ID一一绑定
- 把原脚本的「创建新记录」请求(POST)改为「更新已有记录」请求(PATCH)
修改后的完整脚本
function listAlbumData() { var apiKey = "API_KEY/ACCESS_TOKEN"; // 替换为你的Airtable API密钥 var baseId = "BASE_ID"; // 替换为你的Airtable Base ID var tableName = "tab3"; // 替换为你的表名 // 1. 获取Google Photos所有相册数据 var albums = []; var nextPageToken = null; do { var pageData = getAlbums(nextPageToken); if (pageData.albums && Array.isArray(pageData.albums)) { albums = albums.concat(pageData.albums); } nextPageToken = pageData.nextPageToken; } while (nextPageToken); // 2. 从Airtable获取足够数量的空记录(匹配相册数量) var emptyRecords = getEmptyAirtableRecords(apiKey, baseId, tableName, albums.length); if (emptyRecords.length < albums.length) { throw new Error("Airtable空记录数量不够,请补充足够的空记录"); } // 3. 构造更新用的记录数组(绑定空记录ID和相册数据) var updateRecords = []; albums.forEach(function(album, index) { var record = { "id": emptyRecords[index].id, // 必须指定要更新的记录ID "fields": { "Album Name": album.title, "Number of Photos": album.mediaItemsCount } }; updateRecords.push(record); }); // 4. 批量更新到Airtable updatetable(updateRecords, apiKey, baseId, tableName); } function getAlbums(pageToken) { var options = { method: "GET", headers: { "Authorization": "Bearer " + ScriptApp.getOAuthToken() }, muteHttpExceptions: true }; var url = "https://photoslibrary.googleapis.com/v1/albums"; if (pageToken) { url += "?pageToken=" + pageToken; } var response = UrlFetchApp.fetch(url, options); var data = JSON.parse(response.getContentText()); return data; } // 新增函数:获取Airtable中的空记录(以「Album Name为空」作为判断标准) function getEmptyAirtableRecords(apiKey, baseId, tableName, requiredCount) { var url = "https://api.airtable.com/v0/" + baseId + "/" + tableName; var headers = { "Authorization": "Bearer " + apiKey, "Content-Type": "application/json" }; // 过滤规则:只拉取Album Name为空的记录,最多取requiredCount条 var filterFormula = "IS_BLANK({Album Name})"; var requestUrl = url + "?filterByFormula=" + encodeURIComponent(filterFormula) + "&maxRecords=" + requiredCount; var options = { "method": "GET", "headers": headers }; var response = UrlFetchApp.fetch(requestUrl, options); var data = JSON.parse(response.getContentText()); return data.records || []; } function updatetable(records, apiKey, baseId, tableName) { var url = "https://api.airtable.com/v0/" + baseId + "/" + tableName; var headers = { "Authorization": "Bearer " + apiKey, "Content-Type": "application/json" }; // 按每批10条分组 var batchSize = 10; var batchedRecords = []; while (records.length > 0) { batchedRecords.push(records.splice(0, batchSize)); } // 发送批量更新请求(用PATCH替代原POST) batchedRecords.forEach(function(batch) { var payload = { "records": batch }; var options = { "method": "PATCH", // 关键:更新记录用PATCH,创建用POST "headers": headers, "payload": JSON.stringify(payload), "muteHttpExceptions": true // 方便查看请求错误信息 }; var response = UrlFetchApp.fetch(url, options); // 可选:打印响应日志,方便调试 console.log("批量更新结果:" + response.getContentText()); }); }
关键修改点说明
- 新增空记录获取函数:
getEmptyAirtableRecords会自动筛选Airtable中符合条件的空记录,你可以修改filterFormula来调整空记录的判断标准(比如改为IS_BLANK({Number of Photos}))。 - 绑定记录ID:构造更新数据时必须加入
id字段,这是Airtable识别目标记录的唯一标识。 - 请求方法切换:把原脚本的
POST改为PATCH,这是Airtable API区分「创建」和「更新」的核心。 - 数量校验:加入空记录数量检查,避免因记录不足导致数据无法全部填充。
使用注意事项
- 确保Airtable中空记录数量≥你的Google Photos相册数量
- 替换脚本中所有占位符(API密钥、Base ID、表名)为你自己的信息
- 运行前确认Google Apps Script已授权访问Google Photos和Airtable
内容的提问来源于stack exchange,提问作者Daniel
相关产品推荐
相关产品推荐

