创建电子表格以使用Node js进行读写 [英] Create Spreadsheet for read and write using Node js
问题描述
我需要使用用户身份验证来创建电子表格(如google表格).由于无法找到需要使用哪个NPM模块,我一直坚持着如何启动该项目.
好吗,请帮助任何人将我推向正确的方向?
几周前我一直在从事类似的工作.
这是为您提供的迷你帮助.
1.阅读并遵循
如果您需要更多帮助,请使用Stackoverflow以获得更高的透明度.当您提出非常详细的问题时,Stackoverflow会为您提供帮助.
希望这对您有所帮助,并且您可以开始Google Sheets API之旅.
I Need to create a spreadsheet like a google sheet with user authentication. I have stuck with how to start this project because I couldn't find which NPM module needs to use.
so Kindly help anyone push me in the right direction?
I've been working on similar task few weeks ago.
So here is the mini help for you.
1. Read and follow Node.js Quickstart guide.
Keep an eye on the next concepts like:
- Scopes
For example to get a value from a cell we can use spreadsheets.values.get
Scopes for this method are (use 1 of these):
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.readonly
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/spreadsheets.readonly
in node.js it can be an array
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
- Token
Once you run for the first time your index.js
file the Terminal will ask you for authorization, on finish you will find a token.json
in your work directory.
If you change Scopes -> delete token.json
from your directory
- SpreadsheetId
When you create/edit your spreadsheet you get similar url
https://docs.google.com/spreadsheets/d/1eqRe0ksSwgOoPO0C_mZE6opjS1TlsCU5g1HtkiiILuw/edit#gid=0
1eqRe0ksSwgOoPO0C_mZE6opjS1TlsCU5g1HtkiiILuw
is the ID
2. Create custom functions using Google Sheets API Reference
Example how to get cell values:
const sheets = google.sheets({ version: 'v4', auth });
function getCellsValue(cells, callback){
sheets.spreadsheets.values.get({
spreadsheetId: 'spreadsheetId',
range: cells
}, (err, res) => {
if (err) return console.log('Error #1001 : getCellsValue: ' + err);
const output = res.data.values;
if (output.length) {
callback(output);
return output;
} else {
console.log('No data found.');
}
});
}
// here is the example use of this function which output values from D2:D13
getCellsValue("D2:D13", (e) => {
for(let i = 0; i < e.length; i++){
console.log(e[i].toString());
}
}, (err, res) => {
if (err) return console.error('The API returned an error: ', err.message);
})
3. Make a use of Try this API
This is very useful tool.
In case you need more help just use Stackoverflow for more transparency. Stackoverflow helps you when you make a very detailed question.
I hope this helped you and you can start your Google Sheets API journey.
这篇关于创建电子表格以使用Node js进行读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!