0

I am struggling with how to get the ID from the google script sheet back into the HTML sheet. I am hoping to use the parentfolder ID of the activespreadsheet to be the folder that opens with the picker. The spreadsheet will only be in one folder so will have only 1 parent.

This is the code I was going to use to find the folder ID

function ThisFilePF() {
  var FileID = SpreadsheetApp.getActiveSpreadsheet().getId()
  var ThisFileParentFolders = DriveApp.getFileById(FileID).getParents()
  return ThisFileParentFolders.next().getId()
}

I am then wanting that folder ID (pkrPF) to be used in the HTML sheet when the picker is being built.

function createPicker(token) {
  if (pickerApiLoaded && token) {
    google.script.run.ThisFilePF()
    var PickFolder =  new google.picker.DocsView().setParent(pkrPF)
      .setIncludeFolders(true)
      .setSelectFolderEnabled(true);
    var picker = new google.picker.PickerBuilder()
        .addView(PickFolder)
        .setSelectableMimeTypes('application/vnd.google-apps.folder')
        .enableFeature(google.picker.Feature.NAV_HIDDEN)
        .hideTitleBar()
        .setOAuthToken(token)
        .setDeveloperKey(DEVELOPER_KEY)
        .setCallback(pickerCallback)
        .setOrigin(google.script.host.origin)
        .setSize(DIALOG_DIMENSIONS.width - 2,
                DIALOG_DIMENSIONS.height - 2)
        .build();
    picker.setVisible(true);
  } else {
    showError('Unable to load the file picker.');
  }
}

I know I am making some basic mistakes here, but any pointers would be appreciated.

EDIT: Possibly getting a little closer, but still not sure how to get the variable data into the createpicker function.

I changed google.script.run.ThisFilePF() to google.script.run.withSuccessHandler(getPkrFdrView).ThisFilePF(); and added this function

function getPkrFdrView(pkrPF) {
  pkrPFID = pkrPF
  alert("pkrPFID = " + pkrPFID)
}

The alert popup shows the Folder ID gets pulled through. I just need to work out how that gets to the createpicker function. I have read that you should avoid using Global Variables in javascript, so this is the next hurdle.

Glib
  • 242
  • 2
  • 4
  • 16

1 Answers1

2

Posting an answer so this gets closed.

I fixed it by running the code to get the FileID at the same time as the token was created. That way I already had the data when createpicker runs and could use it in the function. I returned an array from the standard getOAuthCall() as this was where I added the code on the server side.

relevant code snippets:
(HTML Sheet)

</head>
<body>
  <div>
    <p> Please select the property you wish the ticket folder to be created in. </p>
    <button onclick='getOAuthToken()'>Select a Folder</button>
    <br>

...

function getOAuthToken() {
      google.script.run.withSuccessHandler(createPicker)
          .withFailureHandler(showError).getOAuthTokenGS();
    }

...

function createPicker(returned) {
  var token = returned.token
  var pkrPF = returned.pkrPF
  if (pickerApiLoaded && token) {
    var PickFolder =  new google.picker.DocsView().setParent(pkrPF)
      .setIncludeFolders(true)
      .setSelectFolderEnabled(true);

And on the Server Side code.gs sheet
(I had renamed the example function getOAuthToken to getOAuthTokenGS for my understanding)

function getOAuthTokenGS() {
  DriveApp.getRootFolder();
  var token = ScriptApp.getOAuthToken()
  var FileID = SpreadsheetApp.getActiveSpreadsheet().getId()
  var ThisFileParentFolders = DriveApp.getFileById(FileID).getParents()
  var pkrPF = ThisFileParentFolders.next().getId()
  return {token : token, pkrPF : pkrPF}
}

And now when the picker opens, instead of showing the root folder and all the folder on the system, it shows the contents of the folder containing my spreadsheet.

Getting those variables passed from the script to HTML was really confusing me - the callback didn't originally seem to be waiting for the results so this totally baffled me as it is asynchronous. Hope this helps someone else (and if you see something i've done wrong, please shout out).

Glib
  • 242
  • 2
  • 4
  • 16
  • I think that the only issue with your original post is that you were not setting the return value of `google.script.run.ThisFilePF()` to any variable (specifically pkrPF). There are a few ways to do this, but sending the return value to a separate function `createPicker` seems perfectly suitable to me. – Eric Dauenhauer Apr 26 '16 at 00:14