项目作者: cwood821

项目描述 :
A tiny abstraction layer over Google Apps Script Sheets API
高级语言: JavaScript
项目地址: git://github.com/cwood821/sheets-data.git
创建时间: 2019-03-22T14:35:56Z
项目社区:https://github.com/cwood821/sheets-data

开源协议:

下载


Sheets Data

A tiny abstraction layer over Google Apps Script Sheets API

Want to work with Google Spreadsheet Data but keep getting fooled by the API? Don’t get fooled again!

Example

This example assumes a tabular format for spreadsheet data with column headers in the first row. Column headers assumed here are band_name and rating.

  1. // Grab a sheet to pass to SheetsData
  2. var ss = SpreadsheetApp.getActiveSpreadsheet();
  3. var recordsSheet = ss.getSheetByName("records");
  4. // Instantiate a new instance and grab all the data from the sheet
  5. var records = new SheetsData(recordsSheet);
  6. var allMyRecords = records.getAll();
  7. // Get all the records by The Who
  8. var recordsByTheWho = allMyRecords.filter( function(record) {
  9. return record.get("band_name") == "The Who";
  10. });
  11. // Update the rating for each record by The Who to 5
  12. recordsByTheWho.forEach( function(record) {
  13. record.set("rating", 5);
  14. });
  15. // Rock out 🎸

Usage

Instantiate a new instance by passing a sheet

  1. var records = new SheetsData(recordsSheet);

Add a row (record)

SheetsData converts plain-old-JavaScript objects down to a new row when object properties match column headers.

  1. records.add({
  2. "band_name": "Yes",
  3. "rating": 4
  4. });

Grab all rows and columns with data

  1. var allMyRecords = records.getAll()

Get the value of a particular column

  1. // Assumes allMyRecords call from above
  2. allMyRecords[3].get("band_name");

Set the value of a particular column

  1. // Assumes allMyRecords call from above
  2. allMyRecords[3].set("band_name", "Red Hot Chili Peppers");

Store a data into the sheet at an arbitrary location

  1. // Grab a sheet to pass to SheetsData
  2. var ss = SpreadsheetApp.getActiveSpreadsheet();
  3. var recordsSheet = ss.getSheetByName("records");
  4. // Instantiate a new instance and grab all the data from the sheet
  5. var records = new SheetsData(recordsSheet);
  6. var arbitraryRow = 3;
  7. var arbitraryColumn = 4;
  8. records.store(arbitraryRow, arbitraryColumn, "Pinball Wizard");

Benefits

Get and set data from Google Sheets via a simple interface:

  1. record.get(someColumnName);
  2. record.set(someColumnName, newValue);

Use that handy format to map/filter/reduce over the returned data and do stuff.

The Code

The code is written in ES5 due to the limitations of the Google Apps Script Environment. It also needs some cleanup and refactoring.