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

152
Visualizações
Iframe URL insertion from Google Sheet cell

I need Iframe URL to be changed several times a week, buy w/o changing the code. This is what my friend helped me with, but it still doesn't work. Please advise us. Thanks!

<iframe id="magicIframe" src="">
</iframe><script>
    const spreadsheetId = '1pekI9iqMiVkVkRAqajMjmoTfKFsfK1LNTZGcS6NYjRk';
    var iframeUrl = '';
    function sheet() {
        fetch(`https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq?tqx=out:json`)
            .then(res => res.text())
            .then(text => {
                const json = JSON.parse(text.substr(47).slice(0, -2));
                iframeUrl = json.table.rows[0].c[0].v;
            });
        document.getElementById('magicIframe').src = iframeUrl;
    };
    window.onload = function(e) {
        setTimeout(() => {
            sheet();
        }, "2000")

    };
</script>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I seemed to have got it working by moving the line where you inject the iframe URL into the DOM to be a part of your second .then callback:

const spreadsheetId = '1pekI9iqMiVkVkRAqajMjmoTfKFsfK1LNTZGcS6NYjRk';
var iframeUrl = '';

function sheet() {
    fetch(`https://docs.google.com/spreadsheets/d/${spreadsheetId}/gviz/tq?tqx=out:json`)
        .then(res => res.text())
        .then(text => {
            const json = JSON.parse(text.substr(47).slice(0, -2));
            iframeUrl = json.table.rows[0].c[0].v;
            document.getElementById('magicIframe').src = iframeUrl;
            console.log("In scope: " + iframeUrl);
        });

        console.log("Not in scope: " + iframeUrl);
};

window.onload = function(e) {
    setTimeout(() => {
        sheet();
    }, "2000")

};

If you see in the console iframeUrl is still set to the value it was instantiated with. I'm not a JavaScript under the hood expert but it is probably something to do with the way promises and callback functions work? Not sure. I hope this helps

about 4 years ago · Santiago Trujillo 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