0

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.
Kurt Leadley
  • 513
  • 3
  • 20
  • 1
    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`](https://developers.google.com/apps-script/reference/document/document#saveandclose) method which saves the current `Document`. – abielita Aug 20 '16 at 07:38

2 Answers2

1

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?

Community
  • 1
  • 1
some1
  • 857
  • 5
  • 11
  • I'll check this on Monday fyi (weekend off). Thanks for the input. – Kurt Leadley Aug 21 '16 at 04:35
  • 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). – Kurt Leadley Aug 24 '16 at 05:21
0

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.

Kurt Leadley
  • 513
  • 3
  • 20
  • 1
    `var ssNew = SpreadsheetApp.create(targetFilename);` creates a separate new empty file. on the other hand, a single file can definitely be added to multiple folders, all those "instances of that file" are just pointers to the same file and the content will be identical regardless of the parent folder. hope that helps. – some1 Aug 24 '16 at 06:23
  • 2
    very slight amendment to @some1's explanation. Folders don't point to files eg in the sense that a Unix folder contains a list of inodes. On GDrive, each file has a list of parent folders, thus the file points to the folder(s), not the folder(s) point to the file. – pinoyyid Aug 24 '16 at 16:17