谷歌应用程序脚本日历授权需要循环

时间:2013-11-17 13:19:56

标签: calendar google-apps-script

我有一个电子表格,用于存储表单中的数据。一旦数据在表单提交上提交,我就会运行一个脚本,使用谷歌地图进行一些距离计算,并将相关信息通过电子邮件发送给相关的利益相关者。

脚本也运行在电子表格中具有某些数据的列上,然后再将相关信息通过电子邮件发送给相关的利益相关者,并且应该在默认日历中创建一个事件。

脚本一直运行良好,但自从我插入calendarapp.getdefaultcalendar开始创建事件后,我不断获得授权所需的屏幕提示我脚本将管理我的日历,查看和管理我的电子表格,发送电子邮件作为我并连接到外部服务。这一切都很好,我接受提示,除了它一直反复回来。

任何建议请帮忙。我达到了某种类型的配额,它是什么?

function myFunction() {

  var DISTANCE_COLUMN = 14;
  var DURATION_COLUMN = 15;
  var COST_COLUMN = 16;
  var ROUTE_COLUMN = 17;
  var STATE_COLUMN = 18;
  var BOOKED_COLUMN = 19;

  var EMAILED_STATE = "EMAILED";
  var BOOKED_STATE = "BOOKED";

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0]; // gets sheet 0 from active spreadsheet

  var data = getRowsData(sheet); // gets all the data from the active spreadsheet using 
                                 // the getRowsData function below 

  for (var i = 0; i < data.length; i++) { // for every row within the active spreadsheet
  var row = data[i];

  row.rowNumber = i + 2;

if(!row.distance) { // check to see if distance already exists
  var mapsURL = 'http://maps.google.com/maps/api/directions/xml?origin=' + row.pickUpAddress + ' Victoria&destination=' + row.dropOffAddress + ' Victoria&sensor=false&units=metric&mode=driving';
  var mapsXML = UrlFetchApp.fetch(mapsURL).getContentText();
  var mapsResponse = Xml.parse(mapsXML, true);
  var distance = mapsResponse.DirectionsResponse.route.leg.distance.getElement("value").getText();
  var duration = mapsResponse.DirectionsResponse.route.leg.duration.getElement("value").getText();

  var distance = Math.round(distance / 1000); // convert distance in to kilometers
  var duration = Math.round(duration / 60); //convert duration in to minutes
  var cost = 0;


  // Changes the date fields in to strings so that we can compare for tarif one. 
  var formattedPickUpTime = Utilities.formatDate(row.pickUpTime, "GMT+1000", "HHmmss");
  var formattedFromTime = Utilities.formatDate(new Date("12/30/1899 23:00:00"), "GMT+1100", "HHmmss");
  var formattedToTime = Utilities.formatDate(new Date("12/31/1899 04:00:00"), "GMT+1100", "HHmmss");


  var mapsRoute = '"http://maps.google.com/maps?f=d&source=s_d&saddr=' + row.pickUpAddress + ' Victoria&daddr=' + row.dropOffAddress + ' Victoria&mode=driving"';

  sheet.getRange(row.rowNumber, DISTANCE_COLUMN).setValue(distance); // set distance value
  sheet.getRange(row.rowNumber, DURATION_COLUMN).setValue(duration); // set duration value
  sheet.getRange(row.rowNumber, COST_COLUMN).setValue(cost); // set cost value
  sheet.getRange(row.rowNumber, ROUTE_COLUMN).setValue(mapsRoute); // set map of route value
}

if(!row.state) { // check to see if emails have already been sent
  stakeholderEmailFYI(row, distance, duration, cost, mapsRoute); // send an FYI email to the stakeholder
  // customerEmailConfirmation(row); // send a confirmation email to the customer
  sheet.getRange(row.rowNumber, STATE_COLUMN).setValue(EMAILED_STATE); // change state value to SENT      
}

if(row.sendBooking == "Y") { // check to see if emails have already been sent
  // customerEmailConfirmation(row); // send a confirmation email to the customer

  var eventStartTime = row.pickUpTime;
  // End time is calculated by adding an hour in the event start time
  var eventEndTime = new Date(row.pickUpTime.valueOf()+60*60*1000);

  var cal = CalendarApp.getDefaultCalendar().createEvent(row.fullName, eventStartTime, eventEndTime, {location: row.pickUpAddress});

  // cal.createEvent(row.fullName, eventStartTime, eventEndTime, {location: row.pickUpAddress});
  sheet.getRange(row.rowNumber, BOOKED_COLUMN).setValue(BOOKED_STATE); // change state value to SENT
}
}
}

1 个答案:

答案 0 :(得分:2)

尝试撤销授权并重新授权脚本,有时它会解决问题。

这样做:2种可能性:

  1. 您收到一封邮件,告知您已授权此应用,此邮件包含指向自动卸载功能的链接(但这是应用程序由其他人或您的其他帐户编写的时间
  2. 您知道脚本的名称,并且您没有太多脚本,转到您的Google个人资料/安全/撤销访问权限,列出了您授权的所有脚本以及每个脚本的按钮卸载。
  3. (是的我知道我说过2 :)使用你的脚本的url,如下所示:

    https://script.google.com/macros/d/MZMgUL88c4hd6qj_G2Y7-rKtIT_UhbT9n/edit?uiv=2&tz=Europe/Brussels&docTitle=test+sto&csid=tAeLIRLHehPkQGfa6zUYzpg.11577354092571548890.6605670643166911759&mid=ACjPJvG-INu6kmfFfm649tuH-6KYNVpaz5G9uEy4CUMmcKAVn7RYVi8euALNy9dfPWl6gkCloq5D6SKrqczHteazKR2F41v5K5bqbw&hl=en_US

  4. 删除&#34; /编辑后的所有内容?...&#34;包括&#34; /编辑?&#34;

    并添加/manage/uninstall

    这样就变成了这样:

    https://script.google.com/macros/d/MZMgUL88c4h_G2Y7-rKtIT_UhbT9n/manage/uninstall

    这将带您进入如下页面:

    enter image description here

    点击卸载并完成...处女作为处女......

    然后重新授权以确定事情是否会好转。