0
votes

I'm having an issue with the file handling of a Google Sheet that is produced from another Google Sheet when I run a google apps script. When I run the script, a new output file is produced and sent to folders with a specific label. The goal is to have the output file added to a specific folder in a group drive. The script and files regarding my problem all reside on the group drive too fyi.

When I run the script this is what happens.

  1. A file is created within my personal Google Drive because I have a folder with the same exact name as the one on the group drive (this was my test environment for the script). This file on my drive has all the desired output within the Google Sheet. A file was also being added to the root of my drive, but the script now removes it from the root.

  2. A file is created on the group drive, but it is completely empty. The file is correctly named, but there are no contents within the actual spreadsheet.

Here is the script that is handling the output file:

  var sourceFilename = ss.getName();
  var splitSourceFilename = sourceFilename.split("LMS");
  var targetFilename = splitSourceFilename[0] + "Allowed_Layers_Vx";

  // move the new spreadsheet to Allowed Layers Lists folder 
  var folders = DriveApp.getFoldersByName("Allowed Layers Lists");
  while (folders.hasNext()) {
    var folder = folders.next();
    var ssNew = SpreadsheetApp.create(targetFilename);
    var copyFile = DriveApp.getFileById(ssNew.getId());
    folder.addFile(copyFile);
    DriveApp.getRootFolder().removeFile(copyFile);
  }

Some other information about the Group Drive:

  1. I did not create the Group Drive. It is shared with me
  2. I am the owner of the folders in which the files and scripts reside.
  3. I am the owner of all the files and scripts in these folders that I own too.
  4. I have edit permissions at every level of this group drive.
2
Make sure that you correctly configured the editor permissions on the source document/folder. Maybe you're also trashing the file while its copying the data into the new document. You should also check this saveAndClose method which saves the current Document.abielita

2 Answers

1
votes

there is nothing in your script above that actually does the "file copy". It just creating new empty file with SpreadsheetApp.create(targetFilename) and adding that to the folder.

To make a copy of the Google Sheet, you may refer to the answer at Google App Script: How do I make copy of spreadsheet and save it to particular folder?

0
votes

I ended up moving that block of code to the end of the script after the data processing occurs. For some reason, within my own drive, it didn't matter when I added the file to another folder, the script would still update all instances of that file. However, within a group drive, only the file within my own root was being updated (till I moved that block of code to the end of the script).

Any explanations on why this occurs is welcome.