在Apps脚本中获取传递给e.parameter的Blob的字符串值 [英] Get String Value of Blob Passed to e.parameter in Apps Script

查看:85
本文介绍了在Apps脚本中获取传递给e.parameter的Blob的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数提交(e){
var arrayBlob = e.parameter.arrayBlob;
Logger.log(arrayBlob#2 =+ arrayBlob.getDataAsString());

这是我得到的错误:


执行失败:TypeError:
中找不到getDataAsString函数Blob对象.'arrayBlob'



这是我的代码:



<$ p $

如何获得此Blob的字符串值?

p> function showList(folderID){
var folder = DocsList.getFolderById(folderID);
var files = folder.getFiles();
var arrayList = [];
(文件中的var文件){
file = files [file];
var thesesName = file.getName();
var thesesId = file.getId();
var thesesDoc = DocumentApp.openById(thesesId);
for(var child = 0; child< thesesDoc.getNumChildren(); child ++){
var thesesFirstParagraph = thesesDoc.getChild(child);
var thesesType = thesesFirstParagraph.getText();
if(thesesType!=''){
var newArray = [thesesName,thesesType,thesesId];
arrayList.push(newArray);
休息;
}
}
}
arrayList.sort();
var result = userProperties.getProperty('savedArray');
arrayList = JSON.stringify(arrayList);
var arrayBlob = Utilities.newBlob(arrayList);
Logger.log(arrayBlob#1 =+ arrayBlob.getDataAsString()); //这里没关系
var mydoc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication()。setWidth(550).setHeight(450);
var panel = app.createVerticalPanel()
.setId('panel');
panel.add(app.createHidden('arrayBlob',arrayBlob));
var label = app.createLabel(Selecione os itens desejados)。setStyleAttribute(fontSize,18);
app.add(label);
arrayList = JSON.parse(arrayList);
panel.add(app.createHidden('checkbox_total',arrayList.length));
for(var i = 0; i< arrayList.length; i ++){
var checkbox = 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(复选框);
}
var handler = app.createServerHandler('submit')。addCallbackElement(panel);
panel.add(app.createButton('Submit',handler));
var scroll = app.createScrollPanel()。setPixelSize(500,400);
scroll.add(panel);
app.add(scroll);
mydoc.show(app);
}

函数提交(e){
var arrayBlob = e.parameter.arrayBlob;
Logger.log(arrayBlob#2 =+ arrayBlob.getDataAsString());


//继续...
}

我希望这个解决方案能够与多个用户同时使用脚本。

解决方案

我不知道为什么你在这里一直使用Blob,你可以简单地使用JSON。但是,如果您有使用Blob的理由,则可以将JSON数据传递到您的表单中,并在您的处理程序中创建Blob,如下面所修改的代码所示:

  function showList(folderID){
var folder = DocsList.getFolderById(folderID);
var files = folder.getFiles();
var arrayList = [];
(文件中的var文件){
file = files [file];
var thesesName = file.getName();
var thesesId = file.getId();
var thesesDoc = DocumentApp.openById(thesesId);
for(var child = 0; child< thesesDoc.getNumChildren(); child ++){
var thesesFirstParagraph = thesesDoc.getChild(child);
var thesesType = thesesFirstParagraph.getText();
if(thesesType!=''){
var newArray = [thesesName,thesesType,thesesId];
arrayList.push(newArray);
休息;
}
}
}
arrayList.sort();

var result = UserProperties.getProperty('savedArray');

//获取JSON数据通过表单。
var arrayBlob = JSON.stringify(arrayList);

var mydoc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication()。setWidth(550).setHeight(450);
var panel = app.createVerticalPanel()
.setId('panel');

//在表单中包含JSON数据。
panel.add(app.createHidden('arrayBlob',arrayBlob));

var label = app.createLabel(Selecione os itens desejados)。setStyleAttribute(fontSize,18);
app.add(label);

panel.add(app.createHidden('checkbox_total',arrayList.length));
for(var i = 0; i< arrayList.length; i ++){
var checkbox = 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(复选框);
}
var handler = app.createServerHandler('submit')。addCallbackElement(panel);
panel.add(app.createButton('Submit',handler));
var scroll = app.createScrollPanel()。setPixelSize(500,400);
scroll.add(panel);
app.add(scroll);
mydoc.show(app);
}

函数submit(e){
var arrayBlob = Utilities.newBlob(e.parameter.arrayBlob);
Logger.log(arrayBlob#2 =+ arrayBlob.getDataAsString());

//继续...
}

您最初使用的方法,Blob本身从未包含在窗体中,您只是将字符串Blob传递给它。

这是因为函数createHidden(name,value);需要两个字符串作为参数,所以它在arrayBlob对象上调用.toString(),它返回字符串Blob。


I'm using this code to get a blob passed to a function:

function submit(e){
  var arrayBlob = e.parameter.arrayBlob;
  Logger.log("arrayBlob #2 = " + arrayBlob.getDataAsString()); 

This is the error I get:

Execution failed: TypeError: Can not find getDataAsString function in the Blob object.'arrayBlob'

How do I get the string value of this blob?

Here is my code:

function showList(folderID) {
  var folder = DocsList.getFolderById(folderID);
  var files = folder.getFiles();
  var arrayList = [];
  for (var file in files) {
    file = files[file];
    var thesesName = file.getName();
    var thesesId = file.getId();
    var thesesDoc = DocumentApp.openById(thesesId);
    for (var child = 0; child < thesesDoc.getNumChildren(); child++){
    var thesesFirstParagraph = thesesDoc.getChild(child);
    var thesesType = thesesFirstParagraph.getText();
      if (thesesType != ''){
         var newArray = [thesesName, thesesType, thesesId];
         arrayList.push(newArray);
         break;
         }
      }
   }
  arrayList.sort();
  var result = userProperties.getProperty('savedArray');
    arrayList =  JSON.stringify(arrayList);
    var arrayBlob = Utilities.newBlob(arrayList);
    Logger.log("arrayBlob #1 = " + arrayBlob.getDataAsString()); // Here it`s OK
    var mydoc = SpreadsheetApp.getActiveSpreadsheet();
    var app = UiApp.createApplication().setWidth(550).setHeight(450);
    var panel = app.createVerticalPanel()
                   .setId('panel');
    panel.add(app.createHidden('arrayBlob', arrayBlob)); 
    var label = app.createLabel("Selecione os itens desejados").setStyleAttribute("fontSize", 18);
    app.add(label);
    arrayList =  JSON.parse(arrayList);
    panel.add(app.createHidden('checkbox_total', arrayList.length)); 
    for(var i = 0; i < arrayList.length; i++){
      var checkbox = 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);
   }
   var handler = app.createServerHandler('submit').addCallbackElement(panel);
   panel.add(app.createButton('Submit', handler));
   var scroll = app.createScrollPanel().setPixelSize(500, 400);
   scroll.add(panel);
   app.add(scroll);
   mydoc.show(app);
}

function submit(e){
  var arrayBlob = e.parameter.arrayBlob;
  Logger.log("arrayBlob #2 = " + arrayBlob.getDataAsString()); 


 // Continues...
}

I'd like the solution worked with more than one user simultaneous using the script.

解决方案

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) {
  var folder = DocsList.getFolderById(folderID);
  var files = folder.getFiles();
  var arrayList = [];
  for (var file in files) {
    file = files[file];
    var thesesName = file.getName();
    var thesesId = file.getId();
    var thesesDoc = DocumentApp.openById(thesesId);
    for (var child = 0; child < thesesDoc.getNumChildren(); child++){
    var thesesFirstParagraph = thesesDoc.getChild(child);
    var thesesType = thesesFirstParagraph.getText();
      if (thesesType != ''){
         var newArray = [thesesName, thesesType, thesesId];
         arrayList.push(newArray);
         break;
         }
      }
   }
  arrayList.sort();

  var result = UserProperties.getProperty('savedArray');

  //get JSON data pass through form. 
  var arrayBlob = JSON.stringify(arrayList);   

  var mydoc = SpreadsheetApp.getActiveSpreadsheet();
  var app = UiApp.createApplication().setWidth(550).setHeight(450);
  var panel = app.createVerticalPanel()
  .setId('panel');

  //include JSON Data in the form.
  panel.add(app.createHidden('arrayBlob', arrayBlob));  

  var label = app.createLabel("Selecione os itens desejados").setStyleAttribute("fontSize", 18);
  app.add(label);

  panel.add(app.createHidden('checkbox_total', arrayList.length)); 
  for(var i = 0; i < arrayList.length; i++){
    var checkbox = 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);
  }
  var handler = app.createServerHandler('submit').addCallbackElement(panel);
  panel.add(app.createButton('Submit', handler));
  var scroll = app.createScrollPanel().setPixelSize(500, 400);
  scroll.add(panel);
  app.add(scroll);
  mydoc.show(app);
}

function submit(e){
  var arrayBlob = 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".

这篇关于在Apps脚本中获取传递给e.parameter的Blob的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆