1

I am trying to divide a JSON response by a number.

Here's my code:

 function readData() {

var accesstoken = "TOKEN"

 var sheet = SpreadsheetApp.getActiveSheet()
    
 var campaignstatsurl = "https://adsapi.snapchat.com/v1/campaigns/e431cbcd-2281-49fe-8d05-b26c87660eb5/stats?&granularity=TOTAL&fields=impressions,swipes,video_views,view_completion,spend,video_views_time_based"

 var campaignstatsurlresponse = UrlFetchApp.fetch(campaignstatsurl, {
       headers: {
           "Authorization": "Bearer " + accesstoken
       }
   });  

var campaignstatsdata = JSON.parse(campaignstatsurlresponse.getContentText());


var spendvalues = campaignstatsdata.total_stats.map(({ total_stat: { stats } }) => [stats.spend])
sheet.getRange(3, 6, values.length).setValues(spendvalues);

}

I am trying to divide the

 var spendvalues 

By 1,000,000

I have tried the below:

sheet.getRange(3, 6, values.length).setValues(spendvalues/1000000);

I get the following error:

 Exception: The parameters (number) don't match the method signature for SpreadsheetApp.Range.setValues.

Thank you for your help.

Karim
  • 205
  • 1
  • 9
  • From your request of `Hi, could you please have a look at this ? I'm trying to divide my JSON response value by a number :stackoverflow.com/questions/75906184/…` in my previous answer, I saw your question. But, I have to apologize for my poor English skill. Unfortunately, I cannot understand your question. Can I ask you the detail of your question? First, I would like to correctly understand your question. – Tanaike Apr 01 '23 at 12:43
  • @Tanaike Hello and thank you for your kind support. In my code, the output for "sheet.getRange(3, 6, values.length).setValues(spendvalues)" on Google Sheet is "13333330000". I would like to divide that number, by 1,000,000. So I want the output on Google Sheets to be "13,333.33" instead of "13333330000" – Karim Apr 01 '23 at 12:47
  • 1
    Thank you for replying. From your reply, I proposed a modification point as an answer. Please confirm it. If I misunderstood your question, I apologize. – Tanaike Apr 01 '23 at 12:55

1 Answers1

2

From your following reply,

In my code, the output for "sheet.getRange(3, 6, values.length).setValues(spendvalues)" on Google Sheet is "13333330000". I would like to divide that number, by 1,000,000. So I want the output on Google Sheets to be "13,333.33" instead of "13333330000"

I guessed that your value of spendvalues might be a 2-dimensional array like [[123], [456],,,]. If my understanding is correct, I'm worried that this might be the reason for your current issue. So, how about the following modification?

From:

sheet.getRange(3, 6, values.length).setValues(spendvalues);

To:

sheet.getRange(3, 6, spendvalues.length).setValues(spendvalues.map(([e]) => [Number(e) / 1000000])); // or [e / 1000000]

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you sooo much Tanaike! This is what I was asking about. And I confirm that it worked. Thank you very much my friend! – Karim Apr 01 '23 at 12:57
  • I am sorry to bother again. I have a new question where the output is correct but the way I am writing it is inefficient and incorrect. Please have a look whenever you can: https://stackoverflow.com/questions/75907055/how-to-loop-through-json-urls-and-get-information-from-them-inside-google-sheets – Karim Apr 01 '23 at 15:13