gpt4 book ai didi

node.js - Google Sheets API NodeJS - 在不更改格式的情况下更新单元格

转载 作者:行者123 更新时间:2023-12-02 02:46:26 24 4
gpt4 key购买 nike

已编写 NodeJS 代码以使用 API 在 Google 表格中插入行并更新行值。

我插入带有 inheritFromBefore: true 的行,它工作正常(我通过注释掉 updateOption 来测试它)。但是在更新单元格时,原始格式没有得到保留;而是更改为默认的空白格式。

function getSheetRow(jobData) {

const jobID = jobData.getJobID();
const jobName = jobData.getJobName();
const company = jobData.getCompany();
const pmName = jobData.getPMName();

let rowData = [
{ userEnteredValue: { stringValue: jobID } },
{ userEnteredValue: { stringValue: jobName + " (" + company + ", " + pmName + ")" } }
];

return rowData;
}


async function googleSheetInsertRow(workbookID, sheetNum, insertRowIndex, jobData) {
let sheetIdMap = await googleSheetsGetSheetIDTitle(workbookID);
let rowData = getSheetRow(jobData);
return new Promise((resolve, reject) => {

const insertOption = {
insertDimension: {
range: {
sheetId: sheetNum,
dimension: 'ROWS',
startIndex: insertRowIndex,
endIndex: insertRowIndex + 1
},
inheritFromBefore: true
}
};

const updateOption = {
updateCells: {
rows: [{
values: rowData
}],
fields: '*',
start: {
sheetId: sheetNum,
rowIndex: insertRowIndex,
columnIndex: 0
}
}
};

getGSheetsAPI().spreadsheets.batchUpdate({
spreadsheetId: workbookID,
resource: {
requests: [
insertOption, updateOption
]
}
},
(er, re) => {
if (er) {
console.log(`Unable to insert new row into ${googleSheetName} : https://docs.google.com/spreadsheets/d/${workbookID}`);
return reject(er);
}
else {
console.log(`New row inserted into ${googleSheetName} : https://docs.google.com/spreadsheets/d/${workbookID}`);
return resolve();
}
}
);
});
}

[在下图中] 我在第 6 行和第 8 行之间插入了一行。第 7 行继承了之前行的格式,但在更新单元格值时它没有得到保留。

enter image description here

寻求帮助。谢谢。

最佳答案

我认为在您的问题中,当userEnteredValueuserEnteredValue.stringValue 用于fields 时,问题可以得到解决。当您想使用除 stringValue 之外的值时,我建议使用 userEnteredValue 作为字段。

来自:

const updateOption = {
updateCells: {
rows: [{
values: rowData
}],
fields: '*',
start: {
sheetId: sheetNum,
rowIndex: insertRowIndex,
columnIndex: 0
}
}
};

收件人:

const updateOption = {
updateCells: {
rows: [
{
values: rowData,
},
],
fields: "userEnteredValue", // <--- Modified
start: {
sheetId: sheetNum,
rowIndex: insertRowIndex,
columnIndex: 0,
},
},
};

引用:

关于node.js - Google Sheets API NodeJS - 在不更改格式的情况下更新单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62721859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com