7

背景。我想从一周格式更改时间表(每行显示 7 天,从 yyww 中只有一周没有日期可用(例如 1225)。在另一张表中,一列列出周,另一列列出日期。

方法。我将这两张表放入两个数组中,将第三个数组打包,我设置的值放入第三张表中。

问题。此行给出错误消息:“无法转换为。”

  sheet_IndataTabell.getRange(1,1,IndataTable.length+1,7).setValues(IndataTable);

资源。您可以在此处查看文档以及以下完整功能:

function UpdateTable() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet_Indata = ss.getSheetByName("Indata");
var sheet_IndataTabell = ss.getSheetByName("Indata_Tabell");
var sheet_Calendar = ss.getSheetByName("Kalender");

//Get the table into arrays
var Indata = sheet_Indata.getDataRange();
var CalendarTable = sheet_Calendar.getDataRange();

//Gets the values in the Indata to an Array
var NumberRows = Indata.getLastRow();
var NumberCols = Indata.getLastColumn();
//Browser.msgBox(NumberRows + " " + NumberCols);
var IndataArray = new Array(NumberRows,NumberCols);
var IndataArray = Indata.getValues();

//Create an Array to store the result
var IndataTable = new Array(((NumberCols-1)*(NumberRows-1)),7);
//Browser.msgBox("First the IndataTable is defined as: (NumberCols-1)*(NumberRows-1) (" + (NumberCols-1)*(NumberRows-1) + ").");

//Gets the values in the Calendar to an Array
var NumberRows_Cal = CalendarTable.getLastRow();
var NumberCols_Cal = CalendarTable.getLastColumn();
//Browser.msgBox(NumberRows + " " + NumberCols);
var CalendarArray = new Array(NumberRows_Cal,NumberCols_Cal);
var CalendarArray = CalendarTable.getValues();

for (i=1; i<(IndataArray.length); i++)
{

  for (j=3; j<10; j++) {

  IndataTable[i*7+j-3-7] = {};

  //adds the users
  IndataTable[i*7+j-3-7][0] = IndataArray[i][1];

  //adds the week numbers
  var Vecka = IndataArray[i][2];
  IndataTable[i*7+j-3-7][1] = Vecka;

  //adds the hours
  IndataTable[i*7+j-3-7][2] = IndataArray[i][j];

  //adds the projects
  IndataTable[i*7+j-3-7][3] = IndataArray[i][10];

  //adds the day
  var Dag = (j-2);
  IndataTable[i*7+j-3-7][4] = Dag;

  //Gets the date from the Calendar
  IndataTable[i*7+j-3-7][5] = "=VLOOKUP(G" + (i*7+j-9) + ";Kalender!$B$1:C;2)+E" + (i*7+j-9) + "-1";

  IndataTable[i*7+j-3-7][6] = Vecka+"_"+Dag;

  //Browser.msgBox("[" + (i*7+j-3-7) + "][n]: " + IndataTable[i*7+j-3-7][0] + ", " + IndataTable[i*7+j-3-7][1] + ", " + IndataTable[i*7+j-3-7][2] + ", " + IndataTable[i*7+j-3-7][3] + ", " + IndataTable[i*7+j-3-7][4] + ", " + IndataTable[i*7+j-3-7][5] + ", " + IndataTable[i*7+j-3-7][6]);

  }

}


sheet_IndataTabell.clear();
//Browser.msgBox("IndataTable.length: " + IndataTable.length);
//Browser.msgBox("IndataTable[0].length: " + IndataTable[0].length);
//Browser.msgBox("Last row on Range: " + sheet_IndataTabell.getRange(1, 1, IndataTable.length,7).getLastRow());
//Browser.msgBox(IndataTable);

sheet_IndataTabell.getRange("A1:G28").setValues(IndataTable);

//sheet_Indata.sort(3,true); 

//Browser.msgBox("Uppdatering klar!");
sheet_IndataTabell.getRange("J1").setValue(Date());

}
4

2 回答 2

24

setValues()并且getValues()总是使用二维数组,即使范围只有 1 行高,所以你应该使用:

LINE 212     sheet_IndataTabell.getRange(1, 1, 1, 2).setValues([IndataTable_Temp]) 

(注意我添加的那对括号。)

此外,即使这不是问题,您也不需要在使用之前定义数组的大小Range.getValues(),只需删除(NumberRows_Cal,NumberCols_Cal)您作为参数放置的,

var CalendarArray = new Array() 

足够了。

编辑:关于您的评论:请在您的代码中的第 225 行添加这两个 Logger.log,当您获得 2 个数组时,其中一个在 setValues() 中不可用,您会看到发生了什么:

    Logger.log(IndataTable);//this one contains JS objects between {}
    Logger.log(IndataTable_Temp);// this one contains array elements between [[]] 
  //Writes the array content to the sheet
  sheet_IndataTabell.getRange(1, 1, i, 11).setValues(IndataTable_Temp);

EDIT2:我对脚本的末尾做了一个小改动,现在数据是纯字符串,数组是真正的 2D(再次查看括号):

  //Reconstruction the Array, I do not know why this is needed
  var IndataTable_Temp = new Array();
  for (i=0; i<IndataTable.length; i++) {
//  Logger.log(typeof(IndataTable[i][0]))
    IndataTable_Temp.push([
      IndataTable[i][0],
      IndataTable[i][1],
      IndataTable[i][2],
      IndataTable[i][3],
      IndataTable[i][4],
      IndataTable[i][5],
      IndataTable[i][6],
      IndataTable[i][7],
      IndataTable[i][8],
      IndataTable[i][9],
      IndataTable[i][10]
    ]);
  }
    Logger.log(IndataTable)
    Logger.log(IndataTable_Temp)
  //Writes the array content to the sheet
  sheet_IndataTabell.getRange(1, 1, IndataTable_Temp.length, IndataTable_Temp[0].length).setValues(IndataTable_Temp);

  //Sets timestamp
  sheet_IndataTabell.getRange("L1").setValue(Date());
于 2012-06-16T19:16:51.817 回答
0

虽然这是一个旧线程,但我的回答可以帮助其他人在这里解决这个问题,

基本上,这个函数循环遍历一个数组并检查是否有一个空槽,然后它从前一个槽中获取值并向其中添加文本。

function addText(){
  var ss = SpreadsheetApp.getActiveSpreadsheet(); //Getting active spreadsheet
  var s = ss.getActiveSheet();  //Getting active sheet

  var sheetName = s.getSheetName(); //Getting active sheet name
  var lastRow = s.getLastRow(); //Getting sheets last row number

  if (sheetName == "sheetName") { //Checking if active sheet is that we want to be
  var jobValuesRange = s.getRange("C8:C" + lastRow); //Getting range that is only one column
  var jobValues = jobValuesRange.getValues(); //Getting values from only one column

    for (var i = 0; i < jobValues.length + 1; i++) { //Looping through all array values
     if (jobValues[i] == "") { 
       //Here is MAGIC where you must add brackets to create two dimension array.
       //Array should be like with lot of rows like first dimension and second dimension,
       //like one cell in that row. 
       jobValues[i] = [jobValues[i - 1] + " night"]; 
     }
    }
    jobValuesRange.setValues(jobValues); // Here you are just updating old values to new values
  }
}
于 2020-01-10T07:34:43.083 回答