8

I want Google script variable data into Google drive as text file and update that text file regulatory via Google script!

The following code creates a text file and writes the data on it.I wonder how i can update the text file later ?

function createTextFile()
{

  name="testFile.txt";
  name2="testFolder";

  var content = "this is text data to be written in text file";

var dir = DriveApp.getFoldersByName(name2).next()
var file = dir.createFile(name, content);
}
user1788736
  • 2,727
  • 20
  • 66
  • 110
  • I think you've got your names switched in the example. – Tom Woodward Mar 08 '16 at 13:56
  • 1
    Thanks for reply. How i can update the file without creating new one each time i call the function? – user1788736 Mar 08 '16 at 16:09
  • This is unfortunate. I was hoping text files could be appended to. Just like AWS s3, this is not the case, probably because in the backend they're using some kind of NoSQL store. The next best thing is to use a Spreadsheet, which has `appendRow()`: https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendRow(Object). And to everyone who says 'use a database', databases SUCK. Give me a plaintext file anyday #unixphilosophy – Sridhar Sarnobat Apr 11 '18 at 02:29

2 Answers2

22

Just in case anyone's still interested 3 years later... :)

The following will create or append, assuming content is text:

function createOrAppendFile() {
  var fileName="myfile";
  var folderName="myfolder";

  var content = "this is text data to be written in text file";

  // get list of folders with matching name
  var folderList = DriveApp.getFoldersByName(folderName);  
  if (folderList.hasNext()) {
    // found matching folder
    var folder = folderList.next();

    // search for files with matching name
    var fileList = folder.getFilesByName(fileName);

    if (fileList.hasNext()) {
      // found matching file - append text
      var file = fileList.next();
      var combinedContent = file.getBlob().getDataAsString() + content;
      file.setContent(combinedContent);
    }
    else {
      // file not found - create new
      folder.createFile(fileName, content);
    }
  }
}

In summary, as there appears to be no append function, you instead simply read the existing file contents, add the new content, then (over)write the combined content back to the file.

*tip - add a "\n" between the old and new content for a new line separator

Ryan Dugan
  • 563
  • 1
  • 5
  • 8
12

update Override the contents if the file is exists.

function saveData(folder, fileName, contents) {
  var filename = "testFile.txt";

  var children = folder.getFilesByName(filename);
  var file = null;
  if (children.hasNext()) {
    file = children.next();
    file.setContent(contents);
  } else {
    file = folder.createFile(filename, contents);
  }
}

function test() {
  var folders = DriveApp.getFoldersByName("testFolder");
  if (folders.hasNext()) {
    var folder = folders.next();
    saveData(folder, "testfile.txt", "HelloWorld");
    saveData(folder, "testfile.txt", "Welcome");
  }

}
wf9a5m75
  • 6,100
  • 3
  • 25
  • 59
  • Thanks for the code my goal is to create a text file and write data to it. My code does that but i want create another function that if i call that function it opens the same text file and write new data to it (erase old data).I dont want create new text file with same name! Is that possible? How? – user1788736 Mar 08 '16 at 21:45
  • may i know why did you call saveData two times ? you tried to show that old data is overwritten by new data ? it would be nice if you explain the code a little as it will help me understand it better.Thanks for updating the code. – user1788736 Mar 08 '16 at 22:41
  • test() function is an example for that how to use the saveData() function. If you want to test the saveData() function, comment out the second one. – wf9a5m75 Mar 08 '16 at 22:45
  • 1
    Is there any append method as well . In case if i want to append data to text file what should i use ? How i can read data from text file ? – user1788736 Mar 08 '16 at 22:59