googleworkspace / googleworkspace/apps-script-samples

Sheets API appendValues does not use newAppendCellsRequest() and range parameter need clarification.

Open
#522 0 comments 0 reactions 1 assignee View on GitHub

@jpoehnelt is already working on this.

Since Feb 14, 2025.

Dominant language
JavaScript
Stars
5.2k
Forks
2k
PR merge metrics
No merged PRs in 30d

Description

The Google Apps Script snippet for Sheets API Snippets.prototype.appendValues() does not use the provided Sheets.newAppendCellsRequest() sheetId and rows properties.
See

Further, the parameter range JSdoc definition is unclear. It is recommended that the definition be copied from the documentation:

The A1 notation of a range to search for a logical table of data

See docs

There are a number of issues with the Sheets.newAppendCellsRequest() that I have raised in the issue tracker here.

Two possible solutions

Both include updated JSDoc descriptions

1. Includes newAppendCellsRequest() correctly in request.
/**
 * Appends values to the specified range
 * @param {string} spreadsheetId spreadsheet's ID
 * @param {string} range The A1 notation of a range to search for a logical table of data.
 * @param valueInputOption determines how the input should be interpreted
 * @see
 * https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
 * @param {*[][]} _values list of rows of values to input
 * @returns {*} spreadsheet with appended values
 */
Snippets.prototype.appendValues = (spreadsheetId, range,
  valueInputOption, _values) => {
  let values = [
    [
      // Cell values ...
    ]
    // Additional rows ...
  ];
  // [START_EXCLUDE silent]
  values = _values;
  // [END_EXCLUDE]
  try {
    let valueRange = Sheets.newRowData();
    valueRange.values = values;

    let appendRequest = Sheets.newAppendCellsRequest();
    appendRequest.sheetId = spreadsheetId;
    appendRequest.rows = valueRange;

    const result = Sheets.Spreadsheets.Values.append(
      appendRequest.rows, 
      appendRequest.sheetId, 
      range, 
      { valueInputOption: valueInputOption }
    );
    return result;
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', err.message);
  }
};
2. Without newAppendCellsRequest()
/**
 * Appends values to the specified range
 * @param {string} spreadsheetId spreadsheet's ID
 * @param {string} range The A1 notation of a range to search for a logical table of data.
 * @param valueInputOption determines how the input should be interpreted
 * @see
 * https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
 * @param {*[][]} _values list of rows of values to input
 * @returns {*} spreadsheet with appended values
 */
Snippets.prototype.appendValues = (spreadsheetId, range,
  valueInputOption, _values) => {
  let values = [
    [
      // Cell values ...
    ]
    // Additional rows ...
  ];
  // [START_EXCLUDE silent]
  values = _values;
  // [END_EXCLUDE]
  try {
    let valueRange = Sheets.newRowData();
    valueRange.values = values;

    const result = Sheets.Spreadsheets.Values.append(valueRange, spreadsheetId,
      range, {valueInputOption: valueInputOption});
    return result;
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', err.message);
  }
};

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.