【问题标题】:Google Sheets API NodeJS - Update cell without changing formatGoogle Sheets API NodeJS - 在不更改格式的情况下更新单元格
【发布时间】:2020-07-03 19:44:58
【问题描述】:

已编写 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 行继承了之前行的格式,但是在更新单元格值时它没有得到保留。

寻求帮助。谢谢。

【问题讨论】:

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


    【解决方案1】:

    我认为在您的问题中,当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,
        },
      },
    };
    

    参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多