Google脚本基于时间的触发器->一直在寻求授权

时间:2018-08-29 12:29:30

标签: javascript triggers

因此,我有这个Google脚本功能,可以从Google电子表格中读取信息并发送包含数据的电子邮件。我设置了一个基于时间的触发器,该触发器每天在某个小时激活,但问题是,每次触发脚本时,它都会要求授权,所以基本上我收到的是带有此“基于时间”错误的电子邮件进行授权。

当我手动执行该功能并亲自确认授权时,该功能可以完美地正常工作。

我通过代码和脚本的触发选项设置了基于时间的触发,但是它们似乎都不起作用。

你们知道一种始终保持授权以使触发器能够工作并发送带有数据的电子邮件的方法吗?

1 个答案:

答案 0 :(得分:0)

因此要在此处添加一些其他信息,这是我现在正在使用的代码:

电子表格由我和同事两个人共享。

如果我手动执行此脚本,它将要求授权,然后将所需的数据发送到我的电子邮件中。当我尝试设置触发器以自动执行此脚本时,就会出现我的问题。每次触发器执行脚本时,我都会收到一封电子邮件,提示它需要授权。

功能:emailCarts
错误消息:需要授权才能执行此操作。激活:基于时间

function emailCharts(sheet,emails,emailSubject){
var ss = SpreadsheetApp.openById("SPREADSHEET_ID");
var sheet = SpreadsheetApp.openById("SPREADSHEET_ID").getSheetByName("Graficos"); 
var emailSubject = 'Reporte Automático Reclamos y Sugerencias ' + Utilities.formatDate(sheet.getRange("B2").getValue(),ss.getSpreadsheetTimeZone(),"dd/MM/yy");
var emails = 'MY_EMAIL'; // your email ID
var charts = sheet.getCharts();

var sheet2 = SpreadsheetApp.openById("SPREADSHEET_ID").getSheetByName("Tabla");
var schedRange = sheet2.getDataRange();

var body = '<div style="text-align:left;display: inline-block;font-family: 
arial,sans,sans-serif">'
body += '<H1>'+ 'Reclamos Abiertos ' +'</H1>';
body += getHtmlTable(schedRange);
body += '</div><br>';
debugger;  


if(charts.length==0){

  MailApp.sendEmail({to: emails,subject: "ERROR:"+emailSubject,htmlBody: "No hay 
 gráficos en el spreadsheet"});    
  return;

}

var chartBlobs = new Array(charts.length); 
var emailBody = "Estimados,<br><br> Junto con saludar, se hace envío del reporte 
automático de los Reclamos/Sugerencias/Felicitaciones Abiertos y Cerrados, 
correspondiente a la fecha: "
            + Utilities.formatDate(sheet.getRange("B2").getValue(),ss.getSpreadsheetTimeZone(),"dd/MM/yy"); 

var emailImages = {};


for(var i=0;i<charts.length;i++){

  var builder = charts[i].modify();
  builder.setOption('vAxis.format', '#');
  var newchart = builder.build();
  chartBlobs[i] = newchart.getAs('image/png');
  emailBody = emailBody + "<p align='center'><img src='cid:chart"+i+"'></p>";
  emailImages["chart"+i]= chartBlobs[i];

}

  for (var i = 0; i < emails.length; i++){
      MailApp.sendEmail({to: emails[i], subject: emailSubject, htmlBody: emailBody+body, inlineImages: emailImages});
 }

}



function getHtmlTable(range){
  var ss = range.getSheet().getParent();
  var sheet = range.getSheet();
  startRow = range.getRow();
  startCol = range.getColumn();
  lastRow = range.getLastRow();
  lastCol = range.getLastColumn();

  // Read table contents
  var data = range.getValues();

  // Get css style attributes from range
  var fontColors = range.getFontColors();
  var backgrounds = range.getBackgrounds();
  var fontFamilies = range.getFontFamilies();
  var fontSizes = range.getFontSizes();
  var fontLines = range.getFontLines();
  var fontWeights = range.getFontWeights();
  var horizontalAlignments = range.getHorizontalAlignments();
  var verticalAlignments = range.getVerticalAlignments();

  // Get column widths in pixels
  var colWidths = [];
  for (var col=startCol; col<=lastCol; col++) { 
    colWidths.push(sheet.getColumnWidth(col));
  }
  // Get Row heights in pixels
  var rowHeights = [];
  for (var row=startRow; row<=lastRow; row++) { 
    rowHeights.push(sheet.getRowHeight(row));
  }

  // Future consideration...
  var numberFormats = range.getNumberFormats();

  // Build HTML Table, with inline styling for each cell
  var tableFormat = 'style="border:1.5px solid black;border-collapse:collapse;text- align:center" border = 1.5 cellpadding = 5';
  var html = ['<table '+tableFormat+'>'];
  // Column widths appear outside of table rows
  for (col=0;col<colWidths.length;col++) {
    html.push('<col width="'+colWidths[col]+'">')
  }
  // Populate rows
  for (row=0;row<data.length;row++) {
    html.push('<tr height="'+rowHeights[row]+'">');
    for (col=0;col<data[row].length;col++) {
      // Get formatted data
      var cellText = data[row][col];
      if (cellText instanceof Date) {
        cellText = Utilities.formatDate(
                     cellText,
                     ss.getSpreadsheetTimeZone(),
                     "dd/MM/yy");
      }
      var style = 'style="'
                + 'color: ' + fontColors[row][col]+'; '
                + 'font-family: ' + fontFamilies[row][col]+'; '
                + 'font-size: ' + fontSizes[row][col]+'; '
                + 'font-weight: ' + fontWeights[row][col]+'; '
                + 'background-color: ' + backgrounds[row][col]+'; '
                + 'text-align: ' + horizontalAlignments[row][col]+'; '
                + 'vertical-align: ' + verticalAlignments[row][col]+'; '
                +'"';
      html.push('<td ' + style + '>'
                +cellText
                +'</td>');
    }
    html.push('</tr>');
  }
  html.push('</table>');

  return html.join('');
 }