当前位置:首页 > 未分类 > 正文内容

How to Update Multiple Cell Values in Google Sheets with Apps Script

ceacer3周前 (05-02)未分类5896

The SpreadsheetApp service of Google App Script offers the range.setValues() method to update a single cell or a range of cells inside a Google Spreadsheet. You cannot, however, write data to multiple non-consecutive cells in a Spreadsheet using the setValues() method.

The Google Spreadsheet API, available inside Apps Script project through Advanced Google Services, can update multiple cells in one execution. You can write values in single cells, rows, columns or even a 2d matrix of cells.

function updateGoogleSheet(spreadsheetId) {
  /* Written by Amit Agarwal */
  /* Web: ctrlq.org  Email: [email protected] */

  var data = [
    {
      range: 'Sheet1!A1', // Update single cell
      values: [['A1']],
    },
    {
      range: 'Sheet1!B1:B3', // Update a column
      values: [['B1'], ['B2'], ['B3']],
    },
    {
      range: 'Sheet1!C1:E1', // Update a row
      values: [['C1', 'D1', 'E1']],
    },
    {
      range: 'Sheet1!F1:H2', // Update a 2d range
      values: [
        ['F1', 'F2'],
        ['H1', 'H2'],
      ],
    },
  ];

  var resource = {
    valueInputOption: 'USER_ENTERED',
    data: data,
  };

  Sheets.Spreadsheets.Values.batchUpdate(resource, spreadsheetId);
}

相关文章

The Best Google Add

The availability of third-party add-ons for Google Docs, Sheets and Google Slides have certainly mad...

Google Maps Formulas for Google Sheets

Google Maps Formulas for Google Sheets

You can bring the power of Google Maps to your Google Sheets using simple formulas with no coding. Y...

How to Transcribe Audio and Video Attachments in Gmail

How to Transcribe Audio and Video Attachments in Gmail

The Save Gmail to Google Drive add-on lets you automatically download email messages and file attach...

How to Sort Google Sheets Automatically with Apps Script

How to Sort Google Sheets Automatically with Apps Script

This Google Spreadsheet on Udemy courses has about 50 sheets, one for each programming language, and...

How to Use Conditional Logic in Google Documents

How to Use Conditional Logic in Google Documents

Conditional content allows you to customize your Google Docs template and generate different version...

How to Make Phone Numbers Callable in Google Sheets and Docs

How to Make Phone Numbers Callable in Google Sheets and Docs

This tutorial explains how to make phone numbers clickable within Google Sheets, Slides and Google D...