dejinco
dejinco

Reputation: 13

Migrating Code from DocList to DriveApp

I am trying to port the code below to DriveApp but the "doc.append" function does not work when I migrate to "DriveApp.append".

function process(thread, threadStart, folder, pass){

  var start = Date.now();
  var label = folder.getName();
  var html; 

  if(pass > 1){
    var docID = folder.createFile(label + "(part " + pass + ")", '<html>',         MimeType.HTML).getFolderById();
  }
  else{
    var docID = folder.createFile(label + "(part 1)", "<html>",     MimeType.HTML).getFolderById();
  }
  var doc = DocsList.getFolderById(docID);

  try{
     doc.append(globalTOC(total_messages(thread), thread.length, label));
  }
  catch(exception){
    Utilities.sleep(5000);
    doc.append(globalTOC(total_messages(thread), thread.length, label));
    }

Upvotes: 1

Views: 369

Answers (2)

SoftwareTester
SoftwareTester

Reputation: 1090

Ryan Rith wrote 'DriveApp Files or DocsList Files are different objects and should not be used interchangeably'

Maybe the andwer I provided at migrating from docslist to driveapp can help while trying to port the code from DocsList to DriveApp

Upvotes: 0

Ryan Roth
Ryan Roth

Reputation: 1424

The code you have posted has a few issues.

Firstly, it seems to be confusing Folders, Files and File IDs. The first part creates a File in a Folder, but then tries to call getFolderById(). Files do not have a method of this name. It then tries to acquire a File from that ID. If you want a File and its ID, you should just use the original File and call getId() on that:

var myDoc = folder.createFile(myFileName, 
    myHTMLcontents,  MimeType.HTML);
var myDocID = myDoc.getId();

The above will work if you are using DriveApp Files or DocsList Files (which are different objects and should not be used interchangeably).

Secondly, there is currently no append() function available through DriveApp.File. If you need append functionality, one way to do it is to extract the File's contents as string, append to that string, and reset the contents with the new string:

var blob = doc.getBlob();
var content = blob.getDataAsString();
content += '  NEW CONTENT\n';
doc.setContent(content);

Note that setContent() will throw an exception if the content exceeds 10MB.

An alternate approach would be to build your file as a Google Doc, appending paragraphs as needed, and eventually covert that Doc to the file type you need.

Upvotes: 1

Related Questions