Javascript 谷歌脚本-从网站论坛解析HTML-并将数据写入工作表
我从论坛url获取HTML,并从用户的个人资料页面解析用户的帖子数量。我不知道如何将解析后的数字写入谷歌电子表格Javascript 谷歌脚本-从网站论坛解析HTML-并将数据写入工作表,javascript,html,parsing,google-apps-script,google-sheets,Javascript,Html,Parsing,Google Apps Script,Google Sheets,我从论坛url获取HTML,并从用户的个人资料页面解析用户的帖子数量。我不知道如何将解析后的数字写入谷歌电子表格 function msg(message){ Browser.msgBox(message); } function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu("Update") .addItem('Update Table', 'updatePosts') .addToU
function msg(message){
Browser.msgBox(message);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Update")
.addItem('Update Table', 'updatePosts')
.addToUi();
}
function getPostCount(profileUrl){
var html = UrlFetchApp.fetch(profileUrl).getContentText();
var sliced = html.slice(0,html.search('Posts Per Day'));
sliced = sliced.slice(sliced.search('<dt>Total Posts</dt>'),sliced.length);
postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
return postCount;
}
function updatePosts(){
if(arguments[0]===false){
showAlert = false;
} else {
showAlert=true;
}
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var accountSheet = spreadSheet.getSheetByName("account-stats");
var statsLastCol = statsSheet.getLastColumn();
var accountCount = accountSheet.getLastRow();
var newValue = 0;
var oldValue = 0;
var totalNewPosts = 0;
for (var i=2; i<=accountCount; i++){
newValue = parseInt(getPostCount(accountSheet.getRange(i, 9).getValue()));
oldValue = parseInt(accountSheet.getRange(i, 7).getValue());
totalNewPosts = totalNewPosts + newValue - oldValue;
accountSheet.getRange(i, 7).setValue(newValue);
statsSheet.getRange(i,statsLastCol).setValue(newValue-todaysValue);
}
if(showAlert==false){
return 0;
}
msg(totalNewPosts+" new post found!");
}
function valinar(needle, haystack){
haystack = haystack[0];
for (var i in haystack){
if(haystack[i]==needle){
return true;
}
}
return false;
}
它应该在B列中逐个帐户,直到最后一行,并用count更新A列
脚本没有给我任何错误,但它没有将检索到的值设置到电子表格中
function msg(message){
Browser.msgBox(message);
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("Update")
.addItem('Update Table', 'updatePosts')
.addToUi();
}
function getPostCount(profileUrl){
var html = UrlFetchApp.fetch(profileUrl).getContentText();
var sliced = html.slice(0,html.search('Posts Per Day'));
sliced = sliced.slice(sliced.search('<dt>Total Posts</dt>'),sliced.length);
postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
return postCount;
}
function updatePosts(){
if(arguments[0]===false){
showAlert = false;
} else {
showAlert=true;
}
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var accountSheet = spreadSheet.getSheetByName("account-stats");
var statsLastCol = statsSheet.getLastColumn();
var accountCount = accountSheet.getLastRow();
var newValue = 0;
var oldValue = 0;
var totalNewPosts = 0;
for (var i=2; i<=accountCount; i++){
newValue = parseInt(getPostCount(accountSheet.getRange(i, 9).getValue()));
oldValue = parseInt(accountSheet.getRange(i, 7).getValue());
totalNewPosts = totalNewPosts + newValue - oldValue;
accountSheet.getRange(i, 7).setValue(newValue);
statsSheet.getRange(i,statsLastCol).setValue(newValue-todaysValue);
}
if(showAlert==false){
return 0;
}
msg(totalNewPosts+" new post found!");
}
function valinar(needle, haystack){
haystack = haystack[0];
for (var i in haystack){
if(haystack[i]==needle){
return true;
}
}
return false;
}
函数消息(消息){
msgBox(消息);
}
函数onOpen(){
var ui=SpreadsheetApp.getUi();
ui.createMenu(“更新”)
.addItem('updatetable','updatePosts')
.addToUi();
}
函数getPostCount(profileUrl){
var html=UrlFetchApp.fetch(profileUrl.getContentText();
var sliced=html.slice(0,html.search('Posts Per Day');
sliced=sliced.slice(sliced.search('Total Posts'),sliced.length);
postCount=sliced.slice(sliced.search(“”+“”)长度,sliced.search(“”);
返回后计数;
}
函数updatePosts(){
if(参数[0]==false){
showarert=false;
}否则{
showarert=true;
}
var电子表格=SpreadsheetApp.getActiveSpreadsheet();
var accountSheet=spreadSheet.getSheetByName(“账户统计”);
var statsLastCol=statsheet.getLastColumn();
var accountCount=accountSheet.getLastRow();
var newValue=0;
var-oldValue=0;
var totalNewPosts=0;
对于(var i=2;i,以下是从电子表格中获取URL的方法:
function getPostCount(profileUrl){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var thisSheet = ss.getSheetByName("List1");
var getNumberOfRows = thisSheet.getLastRow();
var urlProfile = "";
var sliced = "";
var A_Column = "";
var arrayIndex = 0;
var rngA2Bx = thisSheet.getRange(2, 2, getNumberOfRows, 1).getValues();
for (var i = 2; i < getNumberOfRows + 1; i++) { //Start getting urls from row 2
//Logger.log('count i: ' + i);
arrayIndex = i-2;
urlProfile = rngA2Bx[arrayIndex][0];
//Logger.log('urlProfile: ' + urlProfile);
var html = UrlFetchApp.fetch(urlProfile).getContentText();
sliced = html.slice(0,html.search('Posts Per Day'));
var postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
sliced = sliced.slice(sliced.search('<dt>Total Posts</dt>'),sliced.length);
postCount = sliced.slice(sliced.search("<dd> ")+"<dd> ".length,sliced.search("</dd>"));
Logger.log('postCount: ' + postCount);
A_Column = thisSheet.getRange(i, 1);
A_Column.setValue(postCount);
};
}
那不行。需要把var
放在前面。var postCount=..
在该功能中:
function updatePosts(){
if(arguments[0]===false){
showAlert = false;
} else {
showAlert=true;
}
代码中没有名为arguments
的数组。在哪里定义了arguments
以及它是如何将任何值放入其中的?您可以共享一个指向电子表格副本的链接吗?@SandyGood谢谢。这很有帮助。如果您想现在就可以删除带有链接的注释。您好,Sandy,谢谢您的帮助。正如我所说的从一个长脚本中剪切和编辑代码,假设我没有任何需要满足的参数,我也会将代码分割。再次提问,在函数getPostCount中,对于所有这些更新,我如何获取profileUrl以处理需要。如果您有空设置这些工作,我将非常感谢您ISE我只需要从一开始就做很多工作。请考虑这个事实,将有更多的帐户,不仅是一个:(谢谢你转到我的个人资料,如果你愿意,给我发一封电子邮件。如果你能把问题缩小到某一行,你可以问另一个问题。非常感谢你试图帮助我。这是我第一次这样做的新链接,假设我是第一次这样做,我将如何代替返回postCount。将它写在字段A2中。依此类推名单上的客户再次非常感谢