googlesheet

김승수·2023년 2월 15일
0

spreadsheetId 기준 불러오는 api

  • range : 범위
  • majorDimension : 기준
const { google } = require("googleapis");

const credentials = require("./credentialsPath");
const scopes = ["https://www.googleapis.com/auth/spreadsheets"];
const spreadsheetId = "your spreadsheetId";
const auth = new google.auth.GoogleAuth({
  credentials,
  scopes,
});
const sheets = google.sheets({ version: "v4", auth });

const getSpreadsheetSingleRange = async (spreadsheetId) => {
  const range = "A1:E11";
  const response = await sheets.spreadsheets.values.get({
    spreadsheetId,
    majorDimension: "rows",
    range,
  });
  const numRows = response.data.values ? response.data.values.length : 0;
  console.log(`${numRows} rows retrieved`);
  console.log(response.data.values);
  return response.data.values;
};

spreadsheetId 기준 작성api

  • range : 범위
  • valueInputOption
    - raw : string
    -USER_ENTERED : sheet ui 기준 파싱
const { google } = require("googleapis");

const credentials = require("./credentialsPath");
const scopes = ["https://www.googleapis.com/auth/spreadsheets"];
const spreadsheetId = "your spreadsheetId";
const auth = new google.auth.GoogleAuth({
  credentials,
  scopes,
});
const sheets = google.sheets({ version: "v4", auth });
const range = "A12:E14";
const valueInputOption = "raw";
const values = [
  ["추1", "추가입니다A1", "추가입니다B1", "추가입니다C1", "추가입니다D1"],
  ["추2", "추가입니다A2", "추가입니다B2", "추가입니다C2", "추가입니다D2"],
];
const postSpreadsheet = async (
  spreadsheetId,
  range,
  valueInputOption,
  values
) => {
  const resource = { values };
  const response = await sheets.spreadsheets.values.update({
    spreadsheetId,
    range,
    valueInputOption,
    resource,
  });
  console.log("%d cells updtaed", response.data.updatedCells);
  console.log(response);
  return response;
};
profile
외부형 확장뇌

0개의 댓글