Get Cell

Script gsheets

by rossmccrann ยท 7/27/2022

  • Submitted by adam-kov Deno
    Created 1301 days ago
    1
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    /** @param range 'A1' notation or 'R1C1' notation (https://developers.google.com/sheets/api/guides/concepts#cell) */
    4
    export async function main(
    5
      gsheets_auth: wmill.Resource<"gsheets">,
    6
      spreadsheetId: string,
    7
      range: string,
    8
      dimension: 'DEFAULT' | 'ROWS' | 'COLUMNS' = 'DEFAULT'
    9
    ) {
    10
    
    
    11
      const token = gsheets_auth["token"];
    12
    
    
    13
      let GET_CELLS_URL =
    14
        `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${range}`;
    15
      if(dimension !== 'DEFAULT') {
    16
        GET_CELLS_URL += `?majorDimension=${dimension}`
    17
      }
    18
    
    
    19
      const response = await fetch(GET_CELLS_URL, {
    20
        method: "GET",
    21
        headers: {
    22
          Authorization: "Bearer " + token,
    23
          "Content-Type": "application/json",
    24
        },
    25
      });
    26
    
    
    27
      const json = await response.json()
    28
      if (json.error) {
    29
        const { code, status, message } = json.error
    30
        throw Error(`\n${code} ${status} - "${message}"`)
    31
      }
    32
      return json;
    33
    }