Search code examples
google-apps-scriptgoogle-speech-api

I need to send an api request to each cell of column A google apps script


I am now using the manual method to send each request from column A All I need is to send the same request to all cells of the same column, and the url must contain a variable for this cell

function aramex() {
sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("aramex");
var A2= sheet.getRange('A2').getValue();
var url = "https://rahatystore.matjrah.store/api/rest_admin/orderhistory/";
  var payload = {
      "order_status_id": 20,
      "notify": 0
    };
  var headers ={
        "contentType" : "application/json",
        "Authorization" : "Bearer token"
  }
var options = {
  "method": "put",
  "headers": headers,
  "payload": payload
};
let response1 = UrlFetchApp.fetch(url+ A2, options);

sheet img

The same method, but for each cell


Solution

  • Something like this:

    function aramex() {
      const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("aramex");
      const sr = 2; //data start row
      const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getValues();
      const url = "https://rahatystore.matjrah.store/api/rest_admin/orderhistory/";
      var payload = {
        "order_status_id": 20,
        "notify": 0
      };
      var headers = {
        "contentType": "application/json",
        "Authorization": "Bearer token"
      }
      var options = {
        "method": "put",
        "headers": headers,
        "payload": payload
      };
      vs.forEach(r => {
        let resp = UrlFetchApp.fetch(url + r[0],options);
    
        //rest of code
      })
    }
      
    

    Second question something like this:

    function aramex() {
      const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("aramex");
      const sr = 2; //data start row
      const vs = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, sh.getLastColumn()).getValues();
      const url = "https://rahatystore.matjrah.store/api/rest_admin/orderhistory/";
      var headers = {
        "contentType": "application/json",
        "Authorization": "Bearer token"
      }
      vs.forEach(r => {
        var payload = {
          "order_status_id": r[2],
          "notify": 0
        };
        var options = {
          "method": "put",
          "headers": headers,
          "payload": payload
        };
        let resp = UrlFetchApp.fetch(url + r[0], options);
    
        //rest of code
      })
    }