0

I am trying to get time entries through ClickUp API using the google apps scripts.

function getTime()
{
const query = new URLSearchParams({
  start_date: '0',
  end_date: '0',
  assignee: '0',
  include_task_tags: 'true',
  include_location_names: 'true',
  space_id: '0',
  folder_id: '0',
  list_id: '0',
  task_id: '0',
  custom_task_ids: 'true',
  team_id: '123'
}).toString();

const teamId = '123';
const resp = fetch(
  `https://api.clickup.com/api/v2/team/${teamId}/time_entries?${query}`,
  {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'tk_49237802_NCN'
    }
  }
);

const data =  resp.text();
console.log(data);

}

However it keeps on showing following error:

ReferenceError: URLSearchParams is not defined

Any reference or help will be highly appreciated

OZi
  • 3
  • 3

2 Answers2

0

I think that your script is for Javascript. That is not Google Apps Script. By this, such an error like ReferenceError: URLSearchParams is not defined occurs. If you want to convert your Javascript to Google Apps Script, how about the following sample script?

Sample script:

Please copy and paste the following script to the script editor of Google Apps Script, and save the script. And, please run myFunction. By this, the script is run.

function myFunction() {
  // Ref: https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
  String.prototype.addQuery = function (obj) {
    return this + Object.keys(obj).reduce(function (p, e, i) {
      return p + (i == 0 ? "?" : "&") +
        (Array.isArray(obj[e]) ? obj[e].reduce(function (str, f, j) {
          return str + e + "=" + encodeURIComponent(f) + (j != obj[e].length - 1 ? "&" : "")
        }, "") : e + "=" + encodeURIComponent(obj[e]));
    }, "");
  }

  const query = {
    start_date: '0',
    end_date: '0',
    assignee: '0',
    include_task_tags: 'true',
    include_location_names: 'true',
    space_id: '0',
    folder_id: '0',
    list_id: '0',
    task_id: '0',
    custom_task_ids: 'true',
    team_id: '123'
  };

  const teamId = '123';
  const baseUrl = `https://api.clickup.com/api/v2/team/${teamId}/time_entries`;
  const url = baseUrl.addQuery(query);
  const resp = UrlFetchApp.fetch(url,
    {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
        Authorization: 'tk_49237802_NCN'
      }
    }
  );
  const data = resp.getContentText();
  console.log(data);
}

Note:

  • This sample script suposes that your showing Javascript works fine as Javascript. Please be careful about this. The request of this sample script is the same with your showing Javascript. But, if an error occurs, please confirm Authorization: 'tk_49237802_NCN', and query, and your URL, again.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you @Tanaike. It works well. However i receive a response like this '{"data":[{"id":"3233087745916663325","task":{"id":"3u4bn2w","name":"Adding subscription","status":{"status":"in review","color":"#ff7800","type":"custom","orderindex":2},"custom_type":null},"wid":"3692463","user":{"id":49405112,"username":"ABC","email":"","color":"#03A2FD","initials":"OZ","profilePicture":null},"billable":false,"start":"1668499584330","end":"1668503184330","duration":"3600000","description":"","tags":' I want to save the response in multiple rows in google sheet. – OZi Dec 12 '22 at 12:05
  • @OZi Thank you for replying. I'm glad your issue was resolved. About `I want to save the response in multiple rows in google sheet.`, I would like to support you. But that is a new question, and that is different from your question. So can you post it as new question? Because when your initial question is changed by comment, other users who see your question are confused. By posting it as new question, users including me can think of it. If you can cooperate to resolve your new issue, I'm glad. Can you cooperate to resolve your new question? – Tanaike Dec 12 '22 at 12:10
0

Tanaike's answer is the way to go. But since all the URL parameters in the question are text strings, and they are not used for anything besides getting the .toString(), perhaps it is worth pointing out that you can accomplish the same like this:

  const query = Object.entries({
    start_date: '0',
    end_date: '0',
    assignee: '0',
    include_task_tags: 'true',
    include_location_names: 'true',
    space_id: '0',
    folder_id: '0',
    list_id: '0',
    task_id: '0',
    custom_task_ids: 'true',
    team_id: '123',
  }).map(row => row.join('='))
    .join('&');
doubleunary
  • 13,842
  • 3
  • 18
  • 51