Google Sheets: How to appendRow with 2d getValues

2019-09-02 02:47发布

问题:

Looking for help on this function.

Background: I am creating a Google Sheets based order form for our company, nothing too large its going to support about 100 stores. The problem lies with the operation to append a row to my data collection sheet, database if you will. I'm having trouble with the .appendRow operation.

function grabOrder(){
    var ordNum = ss.getRange("B2").getValue();
    var storeCode = ss.getRange("B1").getValue();
    var ordValues = ss.getRange("A1:A100").getValues();

    insertInfo(ordNum, storeCode, ordValues); 
}

function insertInfo(ordNum, storeCode ,ordValues){

    ssDb.appendRow([ordNum, storeCode, ordValues]);

}

Result: When running my code it doesn't throw an error, it completes the operation and ordNum, storeCode show up properly. However ordValues appears with this:

[Ljava.lang.Object;@78f4b2bd

Note: The code I have provided is an extremely simplified version of my code, as my only issue lies with the appendRow array.

Potential Solution: I am aware that the getValues grabs a two-dimensional array, and the appenRow will only take one dimensional. My guess is I need to convert the 2d-array into a 1d array and have it stored in a variable? Help please!

SOLVED: Here is what I did:

var orderString = timeStamp + "," + ordNum + "," + clc + "," + orderRng.toString();
var orderValues = orderString.split(",");

I then simply passed the new array like so:

ssDb.appendRow(orderValues);

Hope you guys find use for this in the future.

回答1:

function grabOrder(){
  var ordNum = ss.getRange("B2").getValue();
  var storeCode = ss.getRange("B1").getValue();

  var ordValues = ss.getRange("A1:A100")
    .getValues()
    .map(function (row) {
      return row[0];
    });

  insertInfo(ordNum, storeCode, ordValues);
}

function insertInfo(ordNum, storeCode ,ordValues){
  ssDb.appendRow([ordNum, storeCode].concat(ordValues));
}