Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
10.3k views
in Technique[技术] by (71.8m points)

Google Apps Script - We're sorry, a server error occurred. Please wait a bit and try again

We are running into sporadic errors (more often than not) on a project that generates Google Doc documents based on info entered into Google Sheet spreadsheets.

The Google Apps Script project pulls data (from sheets), caches it in-memory with standard var statements, massages the data a bit (formatting, de-duping, etc), then copies a Doc template and does a bunch of token substitution and also inserts some charts based on the in-memory variables.

We are encountering the following error during a high quantity of executions:

Exception: We're sorry, a server error occurred. Please wait a bit and try again.
    at insertChart(Code:5890:10)
    at processForm(Code:4540:5)

The main method creates several (between 5 - 20 or more) charts depending on parameters entered at run-time by the user (which happens via a standard Web Form created by GAS:

function doGet() {
  return HtmlService
      .createTemplateFromFile('index')
      .evaluate();
}

The web page returned by the above is a very standard GAS HTMLService webapp. It contains a form that allows a user to select a few standard types of reporting criteria (date range, filter by certain types of data, etc).

We are duplicating a "template" Docs file which has a bunch of common text and some tokens that we use as placeholders to find and replace with data loaded from the Sheets. The Sheets are very common spreadsheets with the kind of data you'd imagine: date ranges, selections from drop-down lists, free-text columns, etc. (The filtering info specified by the user is used to query the Sheets for the data we want to "merge" into the new Doc. It's basically a much more complex version of your standard "mail merge".)

  var templateId = 'asdfasdfasdfasdfasdfasdfasdfasdfasdf'; // DEV
  var documentId = DriveApp.getFileById(templateId).makeCopy().getId();
  
  DriveApp.getFileById(documentId).setName('Demo Report - Project Report');
  DriveApp.getFileById(documentId).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.COMMENT);
  
  Logger.log('templateId: ' + templateId);
  Logger.log('documentId: ' + documentId);
    
  var doc = DocumentApp.openById(documentId);
  var body = doc.getBody();

We have the following method which takes the Doc body, placeholder, and formatted data from a chart builder. It then identifies the Doc element token to replace (simply a string in the Doc file), and runs a body.insertImage after that token element.

function insertChart(body, placeholder, chart) {
  if (placeholder.getParent().getType() === DocumentApp.ElementType.BODY_SECTION && typeof chart !== 'undefined' && chart !== null) {
    var offset = body.getChildIndex(placeholder);
    
    //Logger.log(chart);
    
    body.insertImage(offset + 1, chart);
  }
}

We have several helper methods like the following buildStackedColumnChart(). It's meant to wrap the Chart API commands to use a dataTable and Chart Builder to return a specific type of chart (bar, stacked bar, line, etc).

Its resulting chart gets passed into the insertChart() method above.

function buildStackedColumnChart(title, header, data) {
  Logger.log('buildStackedColumnChart');
  Logger.log('title: ' + title);
  Logger.log('header: ' + header);
  Logger.log('data: ' + data);
  
  try {
    var dataTable = Charts.newDataTable();
    
    for (var hr = 0; hr < header.length; hr++) {
      var headerRow = header[hr];
      
      dataTable.addColumn(headerRow[0], headerRow[1]);
    }
    
    for (var r = 0; r < data.length; r++) {
      var row = data[r];
      
      dataTable.addRow(row);
    }
    
    dataTable.build();
    
    
    var chart = Charts.newColumnChart()
      .setDataTable(dataTable)
      .setDimensions(600, 500)
      .setTitle(title)
      .setStacked()
      .build();
    return chart;
  } catch (ex) {
    Logger.log('ex: ' + ex);
    return null;
  }
}

In the main processForm() method (called directly by the webapp menioned above when the user selects their criteria and clicks a "Generate Report" button. We have a few calls to a method which finds the token in the template file (just text with {{}} around it as shown below). It iterates through a hard-coded list of values in the in-memory data variables storing accumulations from the Sheets and creates chartHeader and chartData (which contains the values to be charted in a mechanism the helper methods above can translate back into calls that make sense for the Chart API) and uses the insertChart() and `` helper methods to insert the chart after the bookmark token and then remove the bookmark from the document (cleanup so the tokens aren't present in the end report Doc).

  var chartTrainingsSummaryBookmark = findPlaceholder(body, '{{CHART_TRAININGS_SUMMARY}}');
  
  Logger.log('chartTrainingsSummaryBookmark: ' + chartTrainingsSummaryBookmark);
  
  var chartCategories = [
    'Call',
    'Conference Attendee',
    'Meeting (External)',
    'Partnership',
    'Quarterly Board Meeting',
    'Alliance or Workgroup',
    'Training (Others)',
    'Training (Professional Development)',
    'Youth Training',
    'Webinar or Zoom',
    'Other',
    'Coalition',
    'Email',
    'Face-to-face',
    'Phone',
    'Site Visit',
  ];
  
  //  {Call=10.0, Conference Attendee=10.0, Quarterly Board Meeting=10.0, Alliance or Workgroup=10.0, Coalition=10.0, Meeting (External)=10.0}

  var chartHeaderData = [];
  var chartData = [];
  
  var monthChartHeader = [[Charts.ColumnType.STRING, 'Month']];
  var monthChartData = [monthName];
  for (var cci = 0; cci < chartCategories.length; cci++) {
    var chartCategory = monthName + ':' + chartCategories[cci];
    
    if (getChartData(chartCategory, trainingsChartData) > 0) {
      monthChartHeader.push([Charts.ColumnType.NUMBER, chartCategory.split(':')[1]]);
      monthChartData.push(getChartData(chartCategory, trainingsChartData));
    }
  }
  if (monthChartData.length > 1) {
    if (chartHeaderData.length < 1) {
      chartHeaderData = chartHeaderData.concat(monthChartHeader);
    }
    chartData.push(monthChartData);
  }

  Logger.log('----- CHART - TRAININGS: SUMMARY -----');
  
  if (chartData.length > 0 && chartData[0].length > 1) {
    insertChart(body, chartTrainingsSummaryBookmark, buildStackedColumnChart('', chartHeaderData, chartData));
  }
  chartTrainingsSummaryBookmark.removeFromParent();

We are also seeing slight variations that also error out the document generation with a Timeout. (This process can take a while if there is a lot of data that fits within the parameters which the user specified.)

We were wondering why these errors would happen on a seemingly random interval. We can literally just click the button that makes the call again and it might work, or might error out on a different chart, etc.

This script has been working for the most part (we did encounter a few specific errors) in months previous; however, this "Exception: We're sorry, a server error occurred. Please wait a bit and try again." started occuring last weekend and got far worse on Monday / Tuesday (nearly every execution failed, very few succeeded). It's not happening quite as badly today; but we are seeing a few errors.

Also, it's a bit strange, but copying the GAS Project and executing the copy has thrown the error less frequently than the original project in production.

We have some ideas for workarounds; bu, ideally, we would like to identify a root cause so we can correctly fix the issue.

Thanks,

  • Curtis Spendlove
  • Software Delivery Manager, Cayuse Commercial Services
question from:https://stackoverflow.com/questions/66053055/google-apps-script-were-sorry-a-server-error-occurred-please-wait-a-bit-and

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...