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

标签 node.js google-sheets google-sheets-api

已编写 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/

相关文章:

node.js - 使用 Passport.js 部分保护 API 端点

Google 表格中的日期格式无法按照说明工作

postgresql - 为什么我从 Postgres 数据库中获取 iso 8601 日期?

google-sheets - 从 Google 电子表格的下拉列表中选择多个值

javascript - 容器绑定(bind)脚本出​​现错误 "Expected OAuth 2 access token"

node.js - Cypress 选择 HTML <select> 的特定子级

java - 如何使用 node-java 模块在 .js 文件中导入自己的 java 类?

java - 如何将 googlesheet api 响应存储到 java 中的数组?

node.js - 将 Node.js 配置为记录到文件而不是控制台

google-apps-script - 自定义函数不适用于 ArrayFormula