Edits history of script submission #22415 for ' Update Values (gsheets)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /** 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
     */
    type Gsheets = {
      token: string;
    };
    export async function main(
      gsheets_auth: 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 hugo989 6 days ago