Get String Value Of Blob Passed To E.parameter In Apps Script
Solution 1:
Update:
Add a global variable OUTSIDE of any function:
var arrayBlob = Utilities.newBlob("dummy data");
functionshowList(folderID) {
Code here ....
};
Check that the code has access to the blob:
function submit(e){
Logger.log("arrayBlob.getDataAsString(): " + arrayBlob.getDataAsString());
//More Code . . .
}
This solution eliminates the need of embedding a hidden element in the dialog box with a value of the blob.
You won't need this line:
panel.add(app.createHidden('arrayBlob', arrayBlob));
There are other changes I'd make to the code, but I simply want to show the main issue.
Old Info:
In the function showList()
, the method getDataAsString()
works on the blob named arrayBlob
.
Logger.log("arrayBlob #1 = " + arrayBlob.getDataAsString()); // Here it`s OK
In the function, submit()
, the same method does not work.
var arrayBlob = e.parameter.arrayBlob;
In the function showList()
, the code is assigning a newBlob to the variable arrayBlob
. So arrayBlob
is available to have the getDataAsString()
method used on it.
var arrayBlob = Utilities.newBlob(arrayList);
In the function, submit()
, you are trying to pass the arrayBlob
blob variable into the submit()
function, and reference it with e.parameter
.
If you put a Logger.log()
statement in the submit()
function.
functionsubmit(e){
Logger.log('e: ' + e);
Logger.log('e.parameter` + e.parameter);
var arrayBlob = e.parameter.arrayBlob;
Those Logger.log statements should show something in them. If there is nothing in e.parameter
, then there is nothing for the .getDataAsString()
to work on.
It looks like you are putting the arrayBlob into a hidden panel.
panel.add(app.createHidden('arrayBlob', arrayBlob));
But when the object is getting passed to the submit(e)
function, the arrayBlob
might not be getting put into that object.
So, what I'm saying is, the:
Logger.log("arrayBlob #2 = " + arrayBlob.getDataAsString());
Line may be perfectly good, but there is no arrayBlob
there to work on. This hasn't fixed your problem, but do you think I'm understanding part of what is going on?
Solution 2:
I'm not sure why you are using Blob's here at all, you could simply work with JSON instead.
However, if you have a reason to use Blobs, you can pass the JSON data through your form and create the Blob in your handler, as the modified code below does:
function showList(folderID) {
varfolder= DocsList.getFolderById(folderID);
varfiles= folder.getFiles();
vararrayList= [];
for (var file in files) {
file = files[file];
varthesesName= file.getName();
varthesesId= file.getId();
varthesesDoc= DocumentApp.openById(thesesId);
for (varchild=0; child < thesesDoc.getNumChildren(); child++){
varthesesFirstParagraph= thesesDoc.getChild(child);
varthesesType= thesesFirstParagraph.getText();
if (thesesType != ''){
varnewArray= [thesesName, thesesType, thesesId];
arrayList.push(newArray);
break;
}
}
}
arrayList.sort();
varresult= UserProperties.getProperty('savedArray');
//get JSON data pass through form. vararrayBlob= JSON.stringify(arrayList);
varmydoc= SpreadsheetApp.getActiveSpreadsheet();
varapp= UiApp.createApplication().setWidth(550).setHeight(450);
varpanel= app.createVerticalPanel()
.setId('panel');
//include JSON Data in the form.
panel.add(app.createHidden('arrayBlob', arrayBlob));
varlabel= app.createLabel("Selecione os itens desejados").setStyleAttribute("fontSize", 18);
app.add(label);
panel.add(app.createHidden('checkbox_total', arrayList.length));
for(vari=0; i < arrayList.length; i++){
varcheckbox= app.createCheckBox().setName('checkbox_isChecked_'+i).setText(arrayList[i][0]);
Logger.log("arrayList[i][0] = " + arrayList[i][0]);
Logger.log("arrayList[i] ====> " + arrayList[i]);
panel.add(checkbox);
}
varhandler= app.createServerHandler('submit').addCallbackElement(panel);
panel.add(app.createButton('Submit', handler));
varscroll= app.createScrollPanel().setPixelSize(500, 400);
scroll.add(panel);
app.add(scroll);
mydoc.show(app);
}
function submit(e){
vararrayBlob= Utilities.newBlob(e.parameter.arrayBlob);
Logger.log("arrayBlob #2 = " + arrayBlob.getDataAsString());
// Continues...
}
In the method you were using originally, the Blob itself was never included in the form, you were simply passing the string "Blob" around.
This is because the function createHidden(name, value); expects two strings as parameters, so it calls ".toString()" on the arrayBlob object, which returns the string "Blob".
Post a Comment for "Get String Value Of Blob Passed To E.parameter In Apps Script"