我的任务是编写一个GAS(Google应用程序脚本),可以从API competition.com获取任何网站的uv(唯一身份访问者)。我在我的链接中对域进行了硬编码。但是它必须适用于任何域用户想要的。我是初学者,请帮我解决这个问题。这是我做的:
function fetch() {
var url = 'https://apps.compete.com/sites/abcdef.com/trended/search/?apikey=myapikey&metrics=uv';
var response= UrlFetchApp.fetch(url);// Recieves the response corresponding to above request.
Logger.log('\n\nThe number of unique visitors correponding to months is : \n\n'+response);
}
答案 0 :(得分:0)
这里的问题代表您如何首先询问用户输入,然后结束然后运行您的功能?
这可以通过多种方式完成,但我会给你UiApp选项:
function doGet() {
var app = UiApp.createApplication();
var panel = app.createHorizontalPanel();
var chooseURLLabel = app.createLabel("Input a URL");
var URLValue = app.createTextBox().setId("idURL").setName("idURL");
var visitHandler = app.createServerHandler('getVisitorsFunction').addCallbackElement(panel);
var getVisitors = app.createButton("Get visitors").addClickHandler(visitHandler);
panel
.add(chooseURLLabel)
.add(URLValue)
.add(getVisitors);
app.add(panel);
return app;
}
function getVisitorsFunction(e){
var app = UiApp.getActiveApplication();
var visitors = fetch(e.parameter.idURL);
app.add(app.createLabel(visitors));
return app
}
function fetch(url) {
var response= UrlFetchApp.fetch(url);
return "The number of unique visitors correponding to months is : "+response
}