Maxi713
Maxi713

Reputation: 43

Google Script App - Save file in specific folder

What I'm trying to do here with this script is to read the information of an specific sheet with one column and three rows, then create a txt file with these three rows and save it into an specific folder in drive.

So far, with this little script I was able to create the file I wanted but it does it by default in the root folder.

function export() { 
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheets = ss.getSheetByName("T2001D");
  var values = sheets.getDataRange().getValues();
  var text = "";
  for (var j in values)
  {
    text += values [j][0]+"\n";
  }
  var ff = DriveApp.createFile("T2001D",text,MimeType.PLAIN_TEXT);
  }

My latest attempt was this, I'm trying to make a copy of the file in the root into the folder "T2001D - Test" and then remove it from root. But I'm getting an error saying "No item with the given ID could be found, or you do not have permission to access it. (line 13, file "Code")"

function export() { 
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheets = ss.getSheetByName("T2001D");
  var values = sheets.getDataRange().getValues();
  var text = "";
  for (var j in values)
  {
    text += values [j][0]+"\n";
  }
  var ff = DriveApp.createFile("T2001D",text,MimeType.PLAIN_TEXT);
  var fileID = DriveApp.getFilesByName("T2001D");
  var folderID = DriveApp.getFoldersByName("T2001D - Test");
  var file = DriveApp.getFileById(fileID).getName();
  var folder = DriveApp.getFolderById(folderID);
  var newFile = file.makeCopy(file, folder);
}

I have manager access to the folder, so I don't think it's an issue of permissions. Do you guys have any ideas or suggestions to do this?

And one more thing, I'm not a programer. I've created this frankenstein mostly by some examples I saw on this site and a little "reverser engineering" of mine.

Thanks!

Upvotes: 3

Views: 7185

Answers (1)

Tanaike
Tanaike

Reputation: 201358

  • You want to move the created file from root folder to the specific folder.

If my understanding is correct, how about directly creating the file to the specific folder? I think that there are several answers for your situation. So please think of this as one of them.

Modified script:

function export() { 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheetByName("T2001D");
  var values = sheets.getDataRange().getValues();
  var text = "";
  for (var j in values) {
    text += values [j][0]+"\n";
  }
  var folder = DriveApp.getFoldersByName("T2001D - Test").next();
  var ff = folder.createFile("T2001D",text,MimeType.PLAIN_TEXT);

  // if you want to retrieve the file ID of "T2001D", please use ff.getId()
}

Note:

  • The values retrieved by the methods of getFilesByName() and getFoldersByName() are the iterator.
  • In this modified script supposes that the folder with the name of T2001D - Test is existing only one in your Google Drive. If there are several folders with the same name, please tell me. I would like to modify it.

Upvotes: 3

Related Questions