Problem Solved!!
问题解决了! !
So I have been following this mini-guide found here.
所以我一直在跟踪这个在这里找到的迷你指南。
I have inserted the code below for ease of access. However when I run the code (after inputting proper details for google) I get an error at the end.
为了便于访问,我插入了下面的代码。但是,当我运行代码时(在输入正确的谷歌细节之后),最后会出现错误。
Logging into Google...
Logged into Google
Searching for Worksheet 'S'...
JSON Parse Error: SyntaxError: Unexpected end of input
Any idea what I am doing wrong? Is there some change I need to make? Also in the interest of learning, if you could also explain how you solved my problem and not just give me the solution so that I can solve similar problems in the future by myself.
知道我做错了什么吗?我需要做些改变吗?同样为了学习的兴趣,如果你也能解释你是如何解决我的问题而不仅仅是给我解决方案,这样我就能自己解决类似的问题。
Thanks
谢谢
Uk
英国
/**
* Fetch data from the web and save it into a Google Sheet document
*
* Requirements:
1. NodeJS
2. npm install request
3. npm install cheerio
4. npm install edit-google-spreadsheet
* Author: Ido Green | plus.google.com/+Greenido
* Date: 15 Aug 2013
*/
var request = require('request');
var cheerio = require('cheerio');
var Spreadsheet = require('edit-google-spreadsheet');
// Some parameters
// TODO: move from the global scope
var ticker = "LVS";
var yUrl = "http://finance.yahoo.com/q/ks?s=" + ticker;
var financeDetails = new Array();
var keyStr = new Array();
// Upload our data to G-Sheet
function sheetReady(err, spreadsheet) {
if (err) throw err;
spreadsheet.add({ 1: { 1: "Attribute" } });
spreadsheet.add({ 1: { 2: "Value" } });
spreadsheet.add({
2: {
1: keyStr
}
});
spreadsheet.add({
2: {
2: financeDetails
}
});
spreadsheet.send(function(err) {
if(err) throw err;
console.log("Updated " + financeDetails.length + " Items with data");
});
}
//
// The main call to fetch the data, parse it and work on it.
//
request(yUrl, function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
// the keys - We get them from a certain class attribute
var td = $('.yfnc_tablehead1');
$(td).each(function(j, val) {
keyStr[j] = $(val).text();
});
// the values
// TODO: normalize them
var tData = $('.yfnc_tabledata1');
$(tData).each(function(j, val) {
financeDetails[j] = $(val).text();
});
// Let's do something with the data we have
for (var i=0; i < financeDetails.length; i++) {
console.log (i + ") " + keyStr[i] + " " + financeDetails[i]);
}
// upload our data to Google sheet
// yFinanceDataPerComapy
Spreadsheet.create({
debug: true,
username: 'TODO-fill',
password: 'TODO-fill',
debug: true,
spreadsheetName: 'TODO-yourSheetName',
worksheetName: 'TODO-Sheet1orAbetterName',
callback: sheetReady
});
}
}); // -- end of request --
1 个解决方案
#1
1
This is an error with the edit-google-spreadsheet module reported here. The error is thrown when trying to retrieve private spreadsheets. There are a few options you can take.
这里报告的编辑-google-电子表格模块有一个错误。在试图检索私有电子表格时抛出错误。你可以有几个选择。
- Make the spreadsheet public.
- 公开电子表格。
- Try to use the fixed pull request. It looks like there are still issues that need to be resolved.
- 尝试使用固定的拉动请求。看来还有一些问题需要解决。
- Use the worksheet id instead of the worksheet name. This is a more difficult process:
- Start by opening your spreadsheet in google drive
- 从在谷歌驱动器中打开电子表格开始
- Find the spreadsheet id in the url. Example:
https://docs.google.com/spreadsheets/d/SPREADSHEET_ID_HERE/edit?usp=drive_web
- 在url中找到电子表格id。例如:https://docs.google.com/spreadsheets/d/SPREADSHEET_ID_HERE/edit?usp=drive_web
- List the sources by going to this url:
view-source:https://spreadsheets.google.com/feeds/worksheets/SPREADSHEET_ID_HERE/private/full
- 通过这个url列出源:view-source:https://spreadsheets.google.com/feeds/worksheets/SPREADSHEET_ID_HERE/private/full
- Find the entry from the response (od6 in the below example):
- 从响应中找到条目(下面示例中的od6):
- 使用工作表id而不是工作表名称。这是一个更加困难的过程:首先在谷歌驱动器中打开电子表格,在url中找到电子表格id。例如:https://docs.google.com/spreadsheets/d/SPREADSHEET_ID_HERE/edit?usp=drive_web通过这个url来列出源文件:view-source:https://spreadsheets.google.com/feeds/worksheets/SPREADSHEET_ID_HERE/private/full找到响应的条目(以下示例中的od6):
<entry>
<id>https://spreadsheets.google.com/feeds/worksheets/SPREADSHEET_ID_HERE/private/full/od6</id>
...
<title type='text'>Sheet1</title>
...
</entry>
#1
1
This is an error with the edit-google-spreadsheet module reported here. The error is thrown when trying to retrieve private spreadsheets. There are a few options you can take.
这里报告的编辑-google-电子表格模块有一个错误。在试图检索私有电子表格时抛出错误。你可以有几个选择。
- Make the spreadsheet public.
- 公开电子表格。
- Try to use the fixed pull request. It looks like there are still issues that need to be resolved.
- 尝试使用固定的拉动请求。看来还有一些问题需要解决。
- Use the worksheet id instead of the worksheet name. This is a more difficult process:
- Start by opening your spreadsheet in google drive
- 从在谷歌驱动器中打开电子表格开始
- Find the spreadsheet id in the url. Example:
https://docs.google.com/spreadsheets/d/SPREADSHEET_ID_HERE/edit?usp=drive_web
- 在url中找到电子表格id。例如:https://docs.google.com/spreadsheets/d/SPREADSHEET_ID_HERE/edit?usp=drive_web
- List the sources by going to this url:
view-source:https://spreadsheets.google.com/feeds/worksheets/SPREADSHEET_ID_HERE/private/full
- 通过这个url列出源:view-source:https://spreadsheets.google.com/feeds/worksheets/SPREADSHEET_ID_HERE/private/full
- Find the entry from the response (od6 in the below example):
- 从响应中找到条目(下面示例中的od6):
- 使用工作表id而不是工作表名称。这是一个更加困难的过程:首先在谷歌驱动器中打开电子表格,在url中找到电子表格id。例如:https://docs.google.com/spreadsheets/d/SPREADSHEET_ID_HERE/edit?usp=drive_web通过这个url来列出源文件:view-source:https://spreadsheets.google.com/feeds/worksheets/SPREADSHEET_ID_HERE/private/full找到响应的条目(以下示例中的od6):
<entry>
<id>https://spreadsheets.google.com/feeds/worksheets/SPREADSHEET_ID_HERE/private/full/od6</id>
...
<title type='text'>Sheet1</title>
...
</entry>