Search... Ctrl + K

Update Values (gsheets)
One script reply has been approved by the moderators Verified

Created by adam-kov 25 days ago Used 0 times 0 Points

This script updates values in a Google Sheet.

No comments yet

Points: 0
deno
One script reply has been approved by the moderators
Ap­pro­ved
import * as wmill from "https://deno.land/x/windmill@v1.46.2/mod.ts";

/** Update values(s) in the given range.
 *
 * @param startCell Use 'A1' notation.
 * https://developers.google.com/sheets/api/guides/concepts#cell
 *
 * @param responseValueRenderOption Determines how values in the response should be rendered.
 * https://developers.google.com/sheets/api/reference/rest/v4/ValueRenderOption
 *
 * @param responseDateTimeRenderOption Determines how dates, times, and durations in the response should be rendered.
 * This is ignored if `responseValueRenderOption` is `FORMATTED_VALUE`.
 * https://developers.google.com/sheets/api/reference/rest/v4/DateTimeRenderOption
 */
export async function main(
  gsheets_auth: wmill.Resource<"gsheets">,
  spreadsheetId: string,
  startCell: string,
  values: Array<Array<any>>,
  majorDimension: "ROWS" | "COLUMNS" = "ROWS",
  valueInputOption: "RAW" | "USER_ENTERED" = "USER_ENTERED",
  responseValueRenderOption:
    | "FORMATTED_VALUE"
    | "UNFORMATTED_VALUE"
    | "FORMULA" = "FORMATTED_VALUE",
  responseDateTimeRenderOption: "SERIAL_NUMBER" | "FORMATTED_STRING" =
    "SERIAL_NUMBER",
) {
  const token = gsheets_auth["token"];
  let UPDATE_VALUES_URL =
    `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${startCell}?includeValuesInResponse=true`;
  UPDATE_VALUES_URL += `&valueInputOption=${valueInputOption}`;
  UPDATE_VALUES_URL +=
    `&responseValueRenderOption=${responseValueRenderOption}`;
  UPDATE_VALUES_URL +=
    `&responseDateTimeRenderOption=${responseDateTimeRenderOption}`;
  const body = {
    values,
    majorDimension,
  };

  const response = await fetch(UPDATE_VALUES_URL, {
    method: "PUT",
    body: JSON.stringify(body),
    headers: {
      Authorization: "Bearer " + token,
      "Content-Type": "application/json",
    },
  });

  const json = await response.json();
  if (json.error) {
    let { code, status, message } = json.error;
    throw Error(`\n${code} ${status} - "${message}"\n`);
  }
  return json;
}

Submitted by adam-kov 25 days ago

Edited 25 days ago

No comments yet