Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How do you return the final redirect URL of a website using Google Apps Scripts

sorry if this is a simple question but I was wondering how I would be able to return the redirected URL of a webpage using google sheets.

Sample input webCode = SNSKS21KK

I've attached the code below:

function CHECKWEBSITE(webCode){ 
  if (webCode.map){
    return webCode.map(CHECKWEBSITE);
  } else {
    var url = 'https://www.torpedo7.co.nz/products/' + webCode;
    var redirectedUrl = ?;
    var options = {
      'muteHttpExceptions': true,
      'followRedirects': false
    };
    var response = UrlFetchApp.fetch(url, options);
    var status = response.getResponseCode();
    
    if (redirectedUrl.includes("discontinued") || status == 302) {
      return "Not Online"; 
    } else if (status == 200 || status == 301){ 
      return "Online";
    } else {
      return "Not imported";
    }
  }
} 

Expected redirect output = https://www.torpedo7.co.nz/products/SNSKS21KK/title/2022-e-s-force-fx-80-skis---m12-gw-f80-bindings

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In your situation, how about the following modification?

From:

var url = 'https://www.torpedo7.co.nz/products/' + webCode;
var redirectedUrl = ?;
var options = {
  'muteHttpExceptions': true,
  'followRedirects': false
};
var response = UrlFetchApp.fetch(url, options);
var status = response.getResponseCode();

To:

var url = 'https://www.torpedo7.co.nz/products/' + webCode;
var options = {
  'muteHttpExceptions': true,
  'followRedirects': false
};
var response = UrlFetchApp.fetch(url, options);
var redirectedUrl = response.getAllHeaders().Location;
var status = response.getResponseCode();
  • redirectedUrl is retrieved from Location of the response header.
  • When I tested the modified script using webCode = SNSKS21KK, redirectedUrl is https://www.torpedo7.co.nz/products/SNSKS21KK/title/2022-e-s-force-fx-80-skis---m12-gw-f80-bindings and Online was returned.

Reference:

  • fetch(url, params)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda