英文:
How to find a row by value and update it's content from Google Sheets API in javascript nodejs
问题
我正在开发一个使用Google电子表格作为数据库的Next.js应用程序。该应用程序应该使用Sheets API v4来获取、追加和更新电子表格中的值。我在更新特定行的过程中遇到了困难。我需要找到电子表格中第一列("ID")具有特定值的行,并更新该行中的所有单元格。
我想要标记学生的出勤情况。
这是我的电子表格的样子:
id | name | Attendance |
---|---|---|
30336547 | Burhanuddin | Present |
30336548 | Alexandra | |
30336549 | Andrew | |
30336550 | Anna | |
30336551 | Becky | |
30336552 | Benjamin | |
30336553 | Carl |
我尝试删除所有值并循环数组,以获取匹配值的键。我希望能够使用Google表格API的搜索或查找选项。
我尝试了使用batchUpdateByDataFilter()
来实现Google表格API:
import { google } from "googleapis";
const auth = new google.auth.GoogleAuth({
credentials: {
client_email:
"example@example.iam.gserviceaccount.com", //替换为你的 client_email 值
private_key:
"-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n", //替换为你的 private_key 值
},
scopes: [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/spreadsheets",
],
});
const sheets = google.sheets({
auth,
version: "v4",
});
const sourceSheet = await sheets.spreadsheets.values.batchUpdateByDataFilter({
spreadsheetId: "", //替换为你的 spreadsheetId 值
requestBody: {
valueInputOption: "USER_ENTERED",
data: [
{
dataFilter: {
developerMetadataLookup: {
locationType: "COLUMN",
metadataKey: "id",
metadataValue: "30336547",
visibility: "DOCUMENT",
metadataLocation: {
locationType: "COLUMN",
spreadsheet: false,
},
metadataId: 0,
},
},
values: [["Present"]],
},
],
},
});
res.status(200).json({
success: true,
msg: sourceSheet,
});
请注意替换其中的占位符值,如 client_email
、private_key
和 spreadsheetId
,以匹配你的Google API凭据和电子表格。
英文:
I am working on an Nextjs application that uses a Google Spreadsheet as a database. The application should GET, APPEND and UPDATE values in a spreadsheet, using the Sheets API v4.I have difficulties updating a specific row. I need to find a row that has a specific value in it's first column ("ID") and update all the cells in this row.
I want to mark attendance for the Students
This is how my spreadsheet looks like.
id | name | Attendance |
---|---|---|
30336547 | Burhanuddin | Present |
30336548 | Alexandra | |
30336549 | Andrew | |
30336550 | Anna | |
30336551 | Becky | |
30336552 | Benjamin | |
30336553 | Carl |
I have tried removing all values and looping the array and getting the key of the matching value. I want to use and search or find options from google sheet API.
I tried the implement google sheet API to use batchUpdateByDataFilter()
import { google } from "googleapis";
const auth = new google.auth.GoogleAuth({
credentials: {
client_email:
"example@example.iam.gserviceaccount.com", //Placeholder client_email value
private_key:
"-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n", //Placeholder private_key value
},
scopes: [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/spreadsheets",
],
});
const sheets = google.sheets({
auth,
version: "v4",
});
const sourceSheet = await sheets.spreadsheets.values.batchUpdateByDataFilter({
spreadsheetId: "", //Placeholder private_key value
requestBody: {
valueInputOption: "USER_ENTERED",
data: [
{
dataFilter: {
developerMetadataLookup: {
locationType: "COLUMN",
metadataKey: "id",
metadataValue: "30336547",
visibility: "DOCUMENT",
metadataLocation: {
locationType: "COLUMN",
spreadsheet: false,
},
metadataId: 0,
},
},
values: [["Present"]],
},
],
},
});
res.status(200).json({
success: true,
msg: sourceSheet,
});
答案1
得分: 0
我相信你的目标如下。
- 你想从 Google 电子表格的列“A”中搜索一个值,并想要更新列“C”。
- 你想使用 Node.js 的 googleapis 来实现这个目标。
修改要点:
- 在当前阶段,不能直接使用 REST 资源:spreadsheets.developerMetadata 来搜索值。
- 在这种情况下,需要执行以下流程。
- 从电子表格中检索值。
- 检查这些值。
- 更新电子表格。
当这个流程反映在你的脚本中时,可以考虑以下修改。在这个修改中,使用 Sheets API 的脚本被修改。
修改后的脚本:
const sheets = google.sheets({auth, version: "v4"}); // 这是从你展示的脚本中提取的。
const spreadsheetId = "###"; // 请设置你的电子表格 ID。
const sheetName = "Sheet1"; // 请设置你的工作表名称。
const inputValues = ["30336551", "30336553"]; // 这是一个示例输入值。
const { data: { values }} = await sheets.spreadsheets.values.get({ spreadsheetId, range: sheetName });
await sheets.spreadsheets.values.update({
spreadsheetId,
range: sheetName,
resource: {values: values.map((r) => inputValues.includes(r[0]) ? [r[0], r[1], "Present"] : r)},
valueInputOption: "USER_ENTERED",
});
- 当运行这个脚本时,列“A”中的行,其中包含值[
"30336551"
,"30336553"
](这些值来自你的问题)将被更新。值"Present"
将被放入列“C”。
参考:
英文:
I believe your goal is as follows.
- You want to search for a value from column "A" of Google Spreadsheet and want to update column "C".
- You want to achieve this using googleapis for node.js.
Modification points:
- In the current stage, the values cannot be directly searched with REST Resource: spreadsheets.developerMetadata.
- In this case, it is required to do the following flow.
- Retrieve values from Spreadsheet.
- Check the values.
- Update the Spreadsheet.
When this flow is reflected in your script, how about the following modification? In this modification, the script for using Sheets API is modified.
Modified script:
const sheets = google.sheets({auth, version: "v4"}); // This is from your showing script.
const spreadsheetId = "###"; // Please set your Spreadsheet ID.
const sheetName = "Sheet1"; // Please set your sheet name.
const inputValues = ["30336551", "30336553"]; // This is a sample input value.
const { data: { values }} = await sheets.spreadsheets.values.get({ spreadsheetId, range: sheetName });
await sheets.spreadsheets.values.update({
spreadsheetId,
range: sheetName,
resource: {values: values.map((r) => inputValues.includes(r[0]) ? [r[0], r[1], "Present"] : r)},
valueInputOption: "USER_ENTERED",
});
- When this script is run, the rows of
["30336551", "30336553"]
(These values are from your question.) in column "A" are updated. A value of"Present"
is put into the column "C".
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论