0

I'm trying to create a google sheets document via script, and I'm using the execution API example google provides here:

https://developers.google.com/apps-script/guides/rest/quickstart/android

The only change I've made was a line on the script:

function getFoldersUnderRoot() {

    var sheet = SpreadsheetApp.create("MySheet"); //This line.
    var root = DriveApp.getRootFolder();

    var folders = root.getFolders();

    var folderSet = {};

    while (folders.hasNext()) {

        var folder = folders.next();

        folderSet[folder.getId()] = folder.getName();
    }

    return folderSet;
}

But, even though i can run the code correctly, it just doesnt create the sheet. Maybe some authorization problem? I couldnt find it... Id thank you so much for your help... I really need this to my project.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33

1 Answers1

0

Based from this guidelines, before using the API you need to do the following to meet the above requirements:

  1. Create an Apps Script project to call, with the functions you want to use. The API can also be used to call existing scripts that were created for other projects. Open the project in the Apps Script editor.
  2. Deploy the script project for execution by selecting Publish > Deploy as API executable. Choose a version (or create a new one) and who has access, then click Deploy. The new dialog that opens shows your script's ID, listed under "Current API ID". Make note of this ID — you need to enter it into the application code so that the API knows which script to call. If you need to find it again later, select Publish > Deploy as API executable in the code editor to see it.
  3. Choose a Cloud Platform project and ensure both the calling application and the target script share it. If you use the script's default Cloud Platform project, the calling application should use that project to set up its OAuth credentials. This requires you to have access to the default Cloud Platform project, which may not be the case if the script resides in Team Drive. If you are using a new or existing Cloud Platform Project, you need to switch the Apps Script project to use it if you have not done so already.
  4. Enable the Google Apps Script Execution API in the Cloud Platform project. You can find directions for doing this in the Execution API Java Quickstart.
  5. Create a valid Client ID and client secret for the application in the Cloud Platform project. This is covered in the Execution API Java Quickstart.
  6. In the application code, generate an OAuth access token for the API call. This is not a token the API itself uses, but rather one the script requires. The token must be built using the Client ID and the scopes from the script (in the editor, under File > Project properties > Scopes). This also requires prompting the user to authorize the script. The Google client libraries, while not strictly necessary, can greatly assist in handling OAuth for the application.

Hope this helps.

abielita
  • 13,147
  • 2
  • 17
  • 59