Google Sheets + JS 클래스 = DX 프로토타이핑의 꿈
코드를 해싱할 수 있습니다.
이것은 npm에 대한 우리 코드베이스의 다음 릴리스에서 게시될 예정이지만 지금은 액세스할 수 있습니다in our mono repo directly.
다음은 참조용 코드 인라인입니다.
import { CSVtoArray } from "@lrnwebcomponents/utils/utils.js";
export class gSheetInterface {
constructor(target = null, sheetGids = {}) {
// machineName you want to use => gid from google
this.sheetGids = sheetGids;
// sheet
this.sheet = null;
this.target = target;
}
/**
* load data from sheet via API
*/
async loadSheetData(page) {
return await this.loadCSVData(
`https://docs.google.com/spreadsheets/d/e/${this.sheet}/pub?output=csv&gid=${this.sheetGids[page]}`,
page
);
}
/**
* generate appstore query
*/
async loadCSVData(source, sheet) {
return await fetch(source, {
method: this.method,
})
.then((response) => {
if (response.ok) return response.text();
})
.then((text) => {
return this.handleResponse(text, sheet);
});
}
/**
* Convert from csv text to an array in the table function
*/
async handleResponse(text, sheet) {
// Set helps performantly assemble possible collapsed areas
let table = CSVtoArray(text);
let tmp = table.shift();
let headings = {};
let data = [];
for (var i in tmp) {
headings[tmp[i]] = i;
}
for (var i in table) {
let item = {};
for (var j in headings) {
item[j] = table[i][headings[j]];
}
// push data onto the database of all data we have now as objects
data.push(item);
}
// allow for deeper processing on the data or just return the data found
return typeof this.target[`process${sheet}Data`] === "function"
? this.target[`process${sheet}Data`](table, headings, data)
: data;
}
}
코드에서 이것을 사용하는 단계
this.sheet
값{yourNameYouWant: 0}
의 개체를 호출합니다.gid
를 형성합니다. 웹 구성 요소에 대한 통합 상용구 예제
import { gSheetInterface } from "@lrnwebcomponents/utils/lib/gSheetInterface.js";
export class MyElement extends HTMLElement {
constructor() {
super();
// whatever variable you want for your storage internally
this.database = {};
// instance of the class to interface with, add more
// page gid's to the object with nameYouWant being a key
// so that you can reference it after the fact
this.gSI = new gSheetInterface(this, { nameYouWant: 0 });
this.gSI.sheet = "what-ever-your-shee-id-is-after-publish"; /* replace with your sheetID here */
}
async connectedCallback() {
// loop through all the sheet GIDs and load the data
for (var i in this.gSheet.sheetGids) {
this.database[i] = await this.gSheet.loadSheetData(i);
}
// render the data however you want now that the
// "database" has been populated with values
}
static get tag() {
return "my-element";
}
}
customElements.define(MyElement.tag, MyElement);
구현된 사용 사례 보기
이것은 grade-book element we're currently working on에서 사용되는 것을 볼 수 있습니다. 이렇게 하면
gSheetInterface
클래스의 인스턴스가 생성된 다음 이를 활용하여 채점 인터페이스를 채웁니다(아직 많이 개발 중이지만 기능적임).로드 시퀀스의 짧은 비디오
다음은 약 한 달 전
grade-book
의 짧은 비디오(소리 없음)입니다.전체 Google Docs API 설정 대신 이것을 사용하는 이유
이 대 전체 API 액세스의 제한 사항
이것이 누군가에게 유용했기를 바랍니다. 누구나 쉽게 "데이터베이스"로 이동하여 업데이트할 수 있는 무언가와 인터페이스하는 매우 멋진 방법입니다. 저는 데이터에 접근하고 조작하는 진입 방법에 대한 이런 종류의 낮은 장벽이 마음에 듭니다. 성적표 앱의 UX가 더욱 정교해짐에 따라 향후 동영상과 게시물을 제공할 수 있기를 바랍니다.
Reference
이 문제에 관하여(Google Sheets + JS 클래스 = DX 프로토타이핑의 꿈), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/btopro/google-sheets-js-class-dx-prototyping-dream-25cb텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)