Skip to content

I'm trying to loop pages #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
vazcooo1 opened this issue Dec 17, 2021 · 1 comment
Open

I'm trying to loop pages #60

vazcooo1 opened this issue Dec 17, 2021 · 1 comment

Comments

@vazcooo1
Copy link

Hi,
I'm trying to loop pages, basically what I did is (ignore some custom headers):

`function fetch_orders(sheet_name) {

var ck = '****'

var cs = '****'

var website = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name).getRange("B3").getValue();

var manualDate = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name).getRange("B6").getValue(); // Set your order start date in spreadsheet in cell B6

var m = new Date(manualDate).toISOString();

var product = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet_name).getRange("E3").getValue();      

var stat = 'completed'`

After that the for loop

`for (var page=1;page<5;page++){

  var surl = website + "/wp-json/wc/v3/orders?consumer_key=" + ck + "&consumer_secret=" + cs + "&after=" + m + "&per_page=100" + "&status=" + stat + "&search=" + product + "&page=" + page; 
  var url = surl
  Logger.log(url)

  var options =

      {
          "method": "GET",
          "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
          "muteHttpExceptions": true,

      };

  var result = UrlFetchApp.fetch(url, options);   

    Logger.log(result.getResponseCode())
  if (result.getResponseCode() == 200) {

      var params = JSON.parse(result.getContentText());
      Logger.log(result.getContentText());

  }`

As of right now, it works in the sense it fetches pages 1 to 5, the first page gets looped and onto the sheet and then pages-2-3-etc don't get printed to the sheet.

the for loop contains everything until the removeDuplicates function, that one is outside.

I'm not sure if I should include the removeDuplicates into the loop, I felt the loop should probably only contain the result for the JSON.parse but it didn't work so I expanded.

@icelandicicecreamm
Copy link

Hey there, I did for my sheet as well. Here's what I did,

I added the for loop in the start function and took two inputs.. sheet_name & page

function start_syncv2() {

var breakpoint = 0;
var sheet_name = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('GET from Woo').getName();
for (var page = 1; page != breakpoint; page++) {
  if (fetch_orders == []) {
  page == breakpoint;
} else fetch_orders(sheet_name, page);
}

}

and then I edited the surl in the fetch_orders() function to regenerate with the new page number for every iteration.

function fetch_orders(sheet_name,page) {

var surl = website + "/wp-json/wc/v3/orders?consumer_key=" + ck + "&consumer_secret=" + cs + "&after=" + m + "&page=" + page;

}

I'm sure there's a better way of doing this but this gets the job done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants