1

Create Worksheet

by
Published Jul 27, 2022
Script gsheets Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 12 days ago
1
//native
2

3
type Gsheets = {
4
  token: string;
5
};
6
export async function main(
7
  gsheets_auth: Gsheets,
8
  spreadsheetId: string,
9
  title: string,
10
) {
11
  const token = gsheets_auth["token"];
12

13
  const ADD_WORKSHEET_URL = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}:batchUpdate`;
14

15
  const body = {
16
    requests: [
17
      {
18
        addSheet: {
19
          properties: {
20
            title: title,
21
            gridProperties: {
22
              rowCount: 20,
23
              columnCount: 12,
24
            },
25
            tabColor: {
26
              red: 1.0,
27
              green: 0.3,
28
              blue: 0.4,
29
            },
30
          },
31
        },
32
      },
33
    ],
34
  };
35

36
  const response = await fetch(ADD_WORKSHEET_URL, {
37
    method: "POST",
38
    body: JSON.stringify(body),
39
    headers: {
40
      Authorization: "Bearer " + token,
41
      "Content-Type": "application/json",
42
    },
43
  });
44

45
  return await response.text();
46
}
47

Other submissions
  • Submitted by rossmccrann Deno
    Created 404 days ago
    1
    type Gsheets = {
    2
      token: string;
    3
    };
    4
    export async function main(
    5
      gsheets_auth: Gsheets,
    6
      spreadsheetId: string,
    7
      title: string,
    8
    ) {
    9
      const token = gsheets_auth["token"];
    10
    
    
    11
      const ADD_WORKSHEET_URL = `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}:batchUpdate`;
    12
    
    
    13
      const body = {
    14
        requests: [
    15
          {
    16
            addSheet: {
    17
              properties: {
    18
                title: title,
    19
                gridProperties: {
    20
                  rowCount: 20,
    21
                  columnCount: 12,
    22
                },
    23
                tabColor: {
    24
                  red: 1.0,
    25
                  green: 0.3,
    26
                  blue: 0.4,
    27
                },
    28
              },
    29
            },
    30
          },
    31
        ],
    32
      };
    33
    
    
    34
      const response = await fetch(ADD_WORKSHEET_URL, {
    35
        method: "POST",
    36
        body: JSON.stringify(body),
    37
        headers: {
    38
          Authorization: "Bearer " + token,
    39
          "Content-Type": "application/json",
    40
        },
    41
      });
    42
    
    
    43
      return await response.text();
    44
    }
    45