Google脚本appendRow()失败

时间:2016-03-12 20:17:51

标签: javascript

每次我尝试appendRow()时,我都会在电子表格中找到[Ljava.lang.Object; @ 4ed3710。

function my() {    //does not work
  var ssMASTER = SpreadsheetApp.openById('1e4-----vQX');
  var shMASTER = ssMASTER.getSheetByName('master_request');
  var valuesMASTER = shMASTER.getDataRange().getValues();

  var valuesPermaWrt = new Array();
  valuesPermaWrt.push(["WhatEverItem"]); 
  Logger.log("writing:" + valuesPermaWrt);   //Log: WhatEverItem

  ssMASTER.appendRow([valuesPermaWrt]);     //fails
}

我遵循了elias91的解决方案:

var orderString = timeStamp + "," + ordNum + "," + clc + "," + orderRng.toString();
var orderValues = orderString.split(",");
来自Google Sheets: How to appendRow with 2d getValues array?

像这里创建我的失败版本:

function blablaArray() {    //does not work
  var ssMASTER = SpreadsheetApp.openById('1e61------IuFV');
  var shMASTER = ssMASTER.getSheetByName('master_request');
  var valuesMASTER = shMASTER.getDataRange().getValues();   
  Logger.log("writing:" + valuesMASTER[0]);  
  //Log: [Timestamp, currently, scheduled in, Pin number]
  var preappendMe = valuesMASTER[0].toString();
  var appendMe = new Array();
  var appendMe = preappendMe.split(",");
  ssMASTER.appendRow([appendMe]);     //fails
}

我知道这里描述的是appendRow()https://developers.google.com/apps-script/reference/spreadsheet/sheet#activate。但复制粘贴变量10次似乎是一个黑客而不是程序化的解决方案,所以我希望它通过Array 来完成,而不是像这里通过每个String变量。

function blablaSS() {   //works fine
  var ssMASTER = SpreadsheetApp.openById('1e61-----xAU');
  var shMASTER = ssMASTER.getSheetByName('master_request');
 var singularvalue = "ede";
  ssMASTER.appendRow(["a man", singularvalue, "panama"]);
}

1 个答案:

答案 0 :(得分:0)

在添加到Google表格之前,请尝试在数据上调用JSON.stringify()。

var valuesPermaWrt = new Array();
valuesPermaWrt.push(JSON.stringify(["WhatEverItem"])); 

ssMASTER.appendRow(valuesPermaWrt);