简体   繁体   中英

Pipedrive API GET request setValues of array in Google Sheet not working

I have this array that I'm pulling from Pipedrive using API: [John Smith, [555-867-5309, 444-867-5309], [john@gmail.com, john@yahoo.com]] I want this array to populate to the Google sheet like so:

         |       A        |               B                |                    C               |
=========+========+========+========+========+========+========+========+========+===============
    1    |   John Smith   |   555-867-5309, 444-867-5309   |   john@gmail.com, john@yahoo.com   |

I'm using setValues but am getting an error that the number[] doesn't match the method signature for range.setValues so it's not pasting anything to the sheet.

Here's my code. It works to pull in the data. I just want to pull the three fields (name, phone, and email) of all the data it returns.

var URL = "https://....pipedrive.com";
var API_TOKEN = "...";
function test() {
  var sheeturl = 'https://docs.google.com/spreadsheets/d/...';
  var ss = SpreadsheetApp.openByUrl(sheeturl);
  var leadsSheet = ss.getSheetByName('Leads Sheet');
  var personData = [];
  
  var personurl = URL +'/v1/persons/3812?api_token='+ API_TOKEN;
  var options = {
    "method": "get",
    "contentType": "application/json",
  };
  var response = UrlFetchApp.fetch(personurl, options);
  response = JSON.parse(response.getContentText());
  var name = response.data.name;
  var phone = [response.data.phone.map(phone => phone.value).join(', ')];
  var email = [response.data.email.map(email => email.value).join(', ')];
  var personArray = [name, phone, email];
  Logger.log(personArray);
  personData.push(personArray);
  leadsSheet.getRange(leadsSheet.getLastRow()+1, 5, personData.length, personData[0].length).setValues(personData); //writes to end of sheet
}

The log from this is: [20-10-25 16:29:34:339 EDT] [John Smith, [555-867-5309, 444-867-5309], [john@gmail.com, john@yahoo.com]] but nothing populates to the sheet; it remains blank.

Issue:

personArray returns:

[John Smith, [555-867-5309, 444-867-5309], [john@gmail.com, john@yahoo.com]]

but your goal is to return:

[[John Smith, 555-867-5309,444-867-5309, john@gmail.com,john@yahoo.com]]

where 555-867-5309,444-867-5309 and john@gmail.com,john@yahoo.com is a single string element separated by comma.

To convert: ['555-867-5309', '444-867-5309'] to '555-867-5309,444-867-5309' you can use Array.prototype.toString() .


Solution:

Replace:

var personArray = [name, phone, email];

with:

var personArray = [name,phone.toString(),email.toString()]

Complete snippet:

var URL = "https://....pipedrive.com";
var API_TOKEN = "...";
function test() {
  var sheeturl = 'https://docs.google.com/spreadsheets/d/...';
  var ss = SpreadsheetApp.openByUrl(sheeturl);
  var leadsSheet = ss.getSheetByName('Leads Sheet');
  var personData = [];
  
  var personurl = URL +'/v1/persons/3812?api_token='+ API_TOKEN;
  var options = {
    "method": "get",
    "contentType": "application/json",
  };
  var response = UrlFetchApp.fetch(personurl, options);
  response = JSON.parse(response.getContentText());
  var name = response.data.name;
  var phone = [response.data.phone.map(phone => phone.value).join(', ')];
  var email = [response.data.email.map(email => email.value).join(', ')];
  var personArray = [name,phone.toString(),email.toString()];
  Logger.log(leadsSheet.getLastRow()) // <- this should give the last row with content. Scroll down to your sheet to confirm.
  personData.push(personArray);
  leadsSheet.getRange(leadsSheet.getLastRow()+1, 5, personData.length, personData[0].length).setValues(personData); //writes to end of sheet
}

Minimal reproducible example:

function myFunction(){

const sheet = SpreadsheetApp.getActive().getSheetByName('Leads Sheet');
var personData=[];
var name = 'John Smith' 
var phone = ['555-867-5309', '444-867-5309'];
var email = ['john@gmail.com', 'john@yahoo.com'];
var personArray = [name,phone.toString(),email.toString()];
personData.push(personArray);
sheet.getRange(1,1,personData.length,personData[0].length).setValues(personData);
}

Result:

在此处输入图片说明


Related Article:

What does the range method getValues() return and setValues() accept?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM