"Document is missing (perhaps it was deleted?)" in reference to a document that was just created

My script creates a document, stores the document url in a cell on the spreadsheet, then another function opens said document using that url. I am getting the "Document is missing" error about 70% of the time I attempt to run it. Any ideas? the failure is on line 52.

function resultsDoc() {
  var ssa = SpreadsheetApp;
  var ss = ssa.getActiveSpreadsheet();
  var sheets = ss.getSheets();


  var ui = ssa.getUi();


  var doca = DocumentApp;
  var drive = DriveApp;
  var template = "1uSCtqPwDYM-AtGAu3kzv3ZD0jdXnl12GxmdF_BDNb-M";
  var source = doca.openById(template);
  var resDocCopy = drive.getFileById(template).makeCopy('Tournament Results');
  var resDocId = resDocCopy.getId();
  var resDoc = doca.openById(resDocId);
  var docBody = resDoc.getBody();
  var resDocUrl = resDoc.getUrl();
  sheets.shift();
  for (var dis in sheets) {
    var thisSheet = sheets[dis];
    var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "f"];
    var disLetter = letters[dis];
    var eventa = thisSheet.getName();
    var aOne = thisSheet.getRange('B3').getValue() + "  " + thisSheet.getRange('A3').getValue();
    var aTwo = thisSheet.getRange('B4').getValue() + "  " + thisSheet.getRange('A4').getValue();
    var aThree = thisSheet.getRange('B5').getValue() + "  " + thisSheet.getRange('A5').getValue();
    var aFour = thisSheet.getRange('B6').getValue() + "  " + thisSheet.getRange('A6').getValue();
    var aFive = thisSheet.getRange('B7').getValue() + "  " + thisSheet.getRange('A7').getValue();
    var aSix = thisSheet.getRange('B8').getValue() + "  " + thisSheet.getRange('A8').getValue();

    docBody.replaceText('<<' + disLetter + '>>', eventa);
    docBody.replaceText('<<' + disLetter + '1>>', aOne);
    docBody.replaceText('<<' + disLetter + '2>>', aTwo);
    docBody.replaceText('<<' + disLetter + '3>>', aThree);
    docBody.replaceText('<<' + disLetter + '4>>', aFour);
    docBody.replaceText('<<' + disLetter + '5>>', aFive);
    docBody.replaceText('<<' + disLetter + '6>>', aSix);

  }
  ss.insertSheet("Links");
  var linksSheet = ss.getSheetByName("Links");
  linksSheet.getRange(1, 1).setValue("Link to results page:");
  linksSheet.getRange(2, 1).setValue(resDocUrl);
  Utilities.sleep(9000);

}

function sweepsDoc() {
  var doca = DocumentApp;
  var linksSheet = ss.getSheetByName("Links");
  var resDocUrl = linksSheet.getRange(2, 1).getValue();
  var resDoc = doca.openByUrl(resDocUrl);
  var docBody = resDoc.getBody();

  var sweepsSheet = ss.getSheetByName('Sweeps');

  docBody.replaceText("<<sweeps1>>", sweepsSheet.getRange('B2').getValue());
  docBody.replaceText("<<sweeps2>>", sweepsSheet.getRange('B3').getValue());
  docBody.replaceText("<<sweeps3>>", sweepsSheet.getRange('B4').getValue());


}

1 answer

  • answered 2017-06-17 19:37 James Donnellan

    I'm not 100% sure, but I think not saving the document could be causing a the problem.

    Add the line below to the end of your code that builds the doc.

    resDoc.saveAndClose();