Javascript在不同的函数中引入变量;用于谷歌投标的天气脚本
javascript bring back variable in different function ; for use on the google bid by weather script
我有以下google adwords脚本,基本上通过从wunderground带回天气来自动调整每日adwords投标预算(因为产品依赖于天气)
它工作得很好,这可能是一个简单的解决方案,但这不是我的语言。
目前,它从google adwords带回了实时的每日预算(函数称为'getBudget')。我想做的是从我已经连接的excel表中带回每日预算。您可以从下面的excel工作表中看到,它从excel工作表中带回了活动名称和位置名称。我补充说,它带回了第三列,称为dbudget,这很好,但是
我的问题是这一行
if (keyword.getBudget()) {
keyword.setBudget(keyword.getBudget() * bidMultiplier); }
}
我想把那个变量(dudget)带回这里使用它所以它使用dbudget而不是getbudget
。
if keyword.dbudget()) {
keyword.setBudget(keyword.dbudget() * bidMultiplier); }
}
但它不工作的时刻?
脚本是
----------------
// Register for an API key at http://www.wunderground.com/weather/api/
// and enter the key below.
var WEATHER_UNDERGROUND_API_KEY = "";
// Create a copy of http://goo.gl/IPZo3 and enter the URL below.
var SPREADSHEET_URL = "";
/**
* The code to execute when running the script.
*/
function main() {
var data = getSpreadsheetData(SPREADSHEET_URL);
for (var i = 0; i < data.length; i++) {
var row = data[i];
var campaignName = row[0];
var locationName = row[1];
var dbudget = row[2];
try {
var weather = getWeather(locationName);
Logger.log(format('Weather for {0} is {1} °F, {2} mph, and {3} in of rain. Current Daily Budget of £{4}',
locationName, weather['temp_f'], weather['wind_mph'],
weather['precip_today_in'], dbudget));
} catch (error) {
Logger.log(format('Error getting weather for {0}: {1}', locationName, error));
continue;
}
var bidMultiplier = getBidMultiplier(weather);
if (bidMultiplier != 1) {
Logger.log(format('Setting bids to {0}% for campaign "{1}"',
Math.floor(bidMultiplier * 100), campaignName));
adjustBids(campaignName, bidMultiplier);
} else {
Logger.log(format('No bid changes for campaign "{0}".', campaignName));
}
}
}
/**
* Retrieves the campaign names and weather locations from the spreadsheet.
* @param {string} spreadsheetUrl The URL of the spreadsheet.
* @return {Array.<Array.<string>>} an array of campaign names and location
* names.
*/
function getSpreadsheetData(spreadsheetUrl) {
var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
var sheet = spreadsheet.getSheets()[0];
var range =
sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
return range.getValues();
}
/**
* Retrieves the weather for a given location, using the Weather Underground
* API.
* @param {string} location The location to get the weather for.
* @return {Object.<string, string>} The weather attributes and values, as
* defined in the API.
*/
function getWeather(location) {
var url = format('http://api.wunderground.com/api/{0}/conditions/q/{1}.json',
encodeURIComponent(WEATHER_UNDERGROUND_API_KEY),
encodeURIComponent(location));
var response = UrlFetchApp.fetch(url);
if (response.getResponseCode() != 200) {
throw format('Error returned by API: {1}', response.getContentText());
}
var result = JSON.parse(response.getContentText());
if (!result['current_observation']) {
throw format('Invalid location: {0}', location);
}
return result['current_observation'];
}
/**
* Determines the bid multiplier to use based on the weather data.
* @param {Object} weather The weather data to analyze.
* @return (number) The bid multiplier to apply.
*/
function getBidMultiplier(weather) {
// Higher score means higher bids.
var score = 0;
// Temperature.
if (weather['temp_f'] < 30) {score-= 5;}
else if (weather['temp_f'] >= 30 && weather['temp_f'] <= 35 ) {score-= 4;}
else if (weather['temp_f'] >= 36 && weather['temp_f'] <= 40 ) {score-= 3;}
else if (weather['temp_f'] >= 41 && weather['temp_f'] <= 45 ) {score-= 2;}
else if (weather['temp_f'] >= 46 && weather['temp_f'] <= 50 ) {score-= 1;}
else if (weather['temp_f'] >= 51 && weather['temp_f'] <= 55 ) {score-= 0;}
else if (weather['temp_f'] >= 56 && weather['temp_f'] <= 60 ) {score-= 0;}
else if (weather['temp_f'] >= 61 && weather['temp_f'] <= 65 ) {score+= 1;}
else if (weather['temp_f'] >= 66 && weather['temp_f'] <= 70 ) {score+= 2;}
else if (weather['temp_f'] >= 71 && weather['temp_f'] <= 75 ) {score+= 3;}
else if (weather['temp_f'] >= 76 && weather['temp_f'] <= 80 ) {score+= 4;}
else if (weather['temp_f'] > 81) {score+= 5;}
// Increase/decrease bid by 10% for each score point.
return 1 + (0.1 * score);
}
/**
* Adjusts the bids on all keywords in the campaign using the bid multiplier.
* @param {string} campaignName The name of the campaign.
* @param {number} bidMultiplier The bid multiplier to use.
*/
function adjustBids(campaignName, bidMultiplier, dbudget) {
var selector = AdWordsApp.campaigns().withCondition(
format('CampaignName = "{0}"', campaignName));
var iterator = selector.get();
while (iterator.hasNext()) {
var keyword = iterator.next();
if (keyword.getBudget()) {
keyword.setBudget(keyword.getBudget() * bidMultiplier); }
}
}
/**
* Formats a string using "{0}" style placeholders.
* @param {string} str The format string.
* @param {...string} var_args The values to insert into the format string.
* @return {string} The formatted string.
*/
function format(str, var_args) {
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp('''{' + i + '''}', 'gm');
str = str.replace(reg, arguments[i + 1]);
}
return str;
}
看起来您想要使用您已连接的电子表格中的预算值;而不是从Google AdWords函数返回的预算值。对吗?
如果是这样,那么你需要做的就是将dbudget值从计算它的地方传递到函数中并使用它。
似乎你在main()中计算dbudget,所以你会把它传递给adjustBids()
由于adjustBids的函数声明中已经有dbudget作为第三个参数,因此您不必更改它。相反,您只需更改对adjustBids的调用(来自main函数)
adjustBids(campaignName, bidMultiplier, dbudget);
,然后像这样改变adjustBids函数:
if (dbudget) {
keyword.setBudget(dbudget * bidMultiplier);
}
请记住,这假设您与电子表格中的活动名称与Google广告功能将返回的活动名称保持1:1的关系。如果不匹配,事情就会变得……奇怪。
- 谷歌地图API v3不适用于移动浏览器或PhoneGap
- 谷歌地图根本不适用于Meteor-iOS
- SIMPLE Javascript代码,用于显示谷歌电子表格中单个字段的数据
- JavaScript不适用于internet explorer和Firefox,但适用于谷歌
- JavaScript和CSS样式不适用于我的“;谷歌网站”;页
- 谷歌翻译器javascript不适用于移动设备
- 谷歌工作表中用于获取条目和创建日历事件的脚本示例
- (PHP&JavaScript)a href添加按钮赢得'我不适用于Mozilla,但适用于IE和谷歌
- marquee标签不适用于谷歌铬悬停效果
- 新的谷歌分析事件跟踪代码不适用于我的网站
- 标记动画不适用于谷歌地图API
- 如何在谷歌浏览器中使用step-int和step-over'用于调试Java脚本的Web检查器
- 用于谷歌API的Jquery
- 为什么谷歌地图不适用于gmaps.js+ ie9
- 您是否需要将 API 密钥用于客户端应用程序的谷歌地图 API
- 谷歌图表背景颜色不适用于示例代码
- jQuery append 不适用于谷歌地图
- 谷歌放置API不适用于Meteor
- 用于编辑谷歌背景颜色的Chrome扩展程序
- 为什么不't谷歌用于AES加密的Javascript代码在此代码中工作