Add Rows

Script gsheets Verified

by rossmccrann ยท 7/26/2022

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 hours ago
1
//native
2

3
type Gsheets = {
4
  token: string;
5
};
6
export async function main(
7
  gsheets_auth: Gsheets,
8
  sheet_id: string,
9
  values: Array<Array<any>>,
10
  range: string = "Sheet1",
11
) {
12
  const body = {
13
    values: values,
14
  };
15

16
  const valueInputOption = "USER_ENTERED";
17
  const insertDataOption = "INSERT_ROWS";
18
  const includeValuesInResponse = true;
19
  const APPEND_URL = `https://sheets.googleapis.com/v4/spreadsheets/${sheet_id}/values/${range}:append/?valueInputOption=${valueInputOption}&insertDataOption=${insertDataOption}&includeValuesInResponse=${includeValuesInResponse}`;
20

21
  const token = gsheets_auth["token"];
22

23
  const response = await fetch(APPEND_URL, {
24
    method: "POST",
25
    body: JSON.stringify(body),
26
    headers: {
27
      Authorization: "Bearer " + token,
28
      "Content-Type": "application/json",
29
    },
30
  });
31

32
  const result = await response.json();
33

34
  return { result: result };
35
}
36

Other submissions
  • Submitted by thanghm104 Python3
    Created 1085 days ago
    1
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    2
    
    
    3
    export async function main(
    4
      gsheets_auth: wmill.Resource<"gsheets">,
    5
      sheet_id: string,
    6
      values: Array<Array<any>>,
    7
      range: string = 'Sheet1'
    8
    ) {
    9
      const body = {
    10
        "values": values,
    11
      };
    12
    
    
    13
      const valueInputOption = "USER_ENTERED";
    14
      const insertDataOption = "INSERT_ROWS";
    15
      const includeValuesInResponse = true;
    16
      const APPEND_URL =
    17
        `https://sheets.googleapis.com/v4/spreadsheets/${sheet_id}/values/${range}:append/?valueInputOption=${valueInputOption}&insertDataOption=${insertDataOption}&includeValuesInResponse=${includeValuesInResponse}`;
    18
    
    
    19
      const token = gsheets_auth["token"];
    20
    
    
    21
      const response = await fetch(APPEND_URL, {
    22
        method: "POST",
    23
        body: JSON.stringify(body),
    24
        headers: {
    25
          Authorization: "Bearer " + token,
    26
          "Content-Type": "application/json",
    27
        },
    28
      });
    29
    
    
    30
      const result = await response.json();
    31
    
    
    32
      return { result: result };
    33
    }
  • Submitted by rossmccrann Deno
    Created 393 days ago
    1
    type Gsheets = {
    2
      token: string;
    3
    };
    4
    export async function main(
    5
      gsheets_auth: Gsheets,
    6
      sheet_id: string,
    7
      values: Array<Array<any>>,
    8
      range: string = "Sheet1",
    9
    ) {
    10
      const body = {
    11
        values: values,
    12
      };
    13
    
    
    14
      const valueInputOption = "USER_ENTERED";
    15
      const insertDataOption = "INSERT_ROWS";
    16
      const includeValuesInResponse = true;
    17
      const APPEND_URL = `https://sheets.googleapis.com/v4/spreadsheets/${sheet_id}/values/${range}:append/?valueInputOption=${valueInputOption}&insertDataOption=${insertDataOption}&includeValuesInResponse=${includeValuesInResponse}`;
    18
    
    
    19
      const token = gsheets_auth["token"];
    20
    
    
    21
      const response = await fetch(APPEND_URL, {
    22
        method: "POST",
    23
        body: JSON.stringify(body),
    24
        headers: {
    25
          Authorization: "Bearer " + token,
    26
          "Content-Type": "application/json",
    27
        },
    28
      });
    29
    
    
    30
      const result = await response.json();
    31
    
    
    32
      return { result: result };
    33
    }
    34