Writing data in spreadsheet in google app script

Viewed 38

I'm trying to write a map(key, value) pair to google sheets using app script. I have tried using the method below

var crNew = SpreadsheetApp.create("newWorksheetName",1,1);
var ssNew = SpreadsheetApp.openByUrl(crNew.getUrl());

var x=ssNew.getRange("A1");
x.setValues(apimap.keys,apimap.values);

but I'm getting an error as follows

Exception: The parameters (String,String) don't match the method signature for SpreadsheetApp.Range.setValues. myfunction @ Code.gs:102

Can anyone please tell that where i'm doing wrong.The line 102 is this one x.setValues(apimap.keys,apimap.values);

2 Answers

You are trying to call setValues method with 2 arguments.

Based on documentation this method waits for a two-dimensional array of values as an argument: enter image description here

I have achieved my task as follows (posting it here in case someone faces it in future):

   var crNew = SpreadsheetApp.create("newWorksheetName",1,1);
    var ssNew = SpreadsheetApp.openByUrl(crNew.getUrl());
   
    
    var a=[];
    apimap.forEach (function(value, key) {    //apimap is my map containing data
      a.push([key,value]);    //i pushed data map into an array
    })
    var x=ssNew.getSheetByName("Sheet1").getRange(1,1,a.length,a[0].length);
    x.setValues(a);

This automatically writes keys in one column and values in the adjacent column

Related