1

This code working good, but file goes to the root folder. I not found any helpfully in internet...

function saveAsJSON() {
  var blob,file,fileSets,obj;
  
  obj = {//Object literal for testing purposes
    key:"value"
  }

/**
 * Creates a file in the users Google Drive
 */
  
  fileSets = {
    title: 'file.json',
    mimeType: 'application/json'
  };
  
  blob = Utilities.newBlob(JSON.stringify(obj), "application/vnd.google-apps.script+json");
  file = Drive.Files.insert(fileSets, blob);
  Logger.log('ID: %s, File size (bytes): %s, type: %s', file.id, file.fileSize, file.mimeType);

}

1 Answer 1

1

ANSWER

You need to use the parents property in your fileSets and put your desired drive folder ID, as seen from this sample documentation. See this tweaked script below:

Tweaked Script

function saveAsJSON() {
  var blob,file,fileSets,obj;
  
  obj = {//Object literal for testing purposes
    key:"value"
  }

/**
 * Creates a file in the users Google Drive
 */
  
  fileSets = {
    title: 'file.json',
    mimeType: 'application/json',
    parents:[{"id":"THE_FOLDER_ID"}] //Add your FOLDER_ID here
  };
  
  blob = Utilities.newBlob(JSON.stringify(obj), "application/vnd.google-apps.script+json");
  file = Drive.Files.insert(fileSets, blob);
  Logger.log('ID: %s, File size (bytes): %s, type: %s', file.id, file.fileSize, file.mimeType);
}

Sample Demonstration

  • The file was saved to my sample tempFolder:

enter image description here

2
  • @NearEternal I hope this works on your end. Just let me know if there's anything missing. Commented Feb 22, 2022 at 15:27
  • Perfect! Now it's working, Thanks! Commented Feb 22, 2022 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.