Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
977 views
in Technique[技术] by (71.8m points)

fetch javascript input/output from google sheet

I have the code below on Apps script but keep getting 'ReferenceError: setInterval is not defined on line 17'. Not sure how to fix this but it does load when i use the scipt below in a cell in google sheets and set it up to output to another - it runs apart from that error. not sure if it's because i have it set up wrong or because google apps script doesn't like the code

function scart() {// ==UserScript==


setInterval(function() {
    var possibleClasses = [".listenArtworkWrapper", ".listenInfo"],
        sizes = { 't500x500': '500x500', 'original': 'original size' },
        regexp = /td{3}xd{3}/gi;

$('.modal__content:not(.appeared)')
    .addClass('appeared')
    .each(handleModal);

function handleModal() {
    var imageURL;

    for (var i = 0; i < possibleClasses.length; i++) {
        if ($(possibleClasses[i] + " .image__full").length > 0) {
            imageURL = $(possibleClasses[i] + " .image__full").css('background-image');
        }
    }

    if (!imageURL) {
        logError('No suitable selector found!');
    } else {
        imageURL = /url("(.+)")/.exec(imageURL)[1];
    }

    $(".modal__content .image__full").parent().remove();
    $(".modal__content .imageContent").append("<img style='width: 500px; height: 500px; margin-bottom: 15px;' src='" + imageURL.replace(regexp, 't500x500') + "'>");

    Object.keys(sizes).forEach(function (size) {
        var url = imageURL.replace(regexp, size);
        $.ajax({
            type: 'HEAD',
            url: url,
            complete: function(xhr) {
                if (xhr.status !== 200) {
                    return;
                }

                $(".modal__content .imageContent").append(
                    makeButton(url, sizes[size])
                );
            }
        });
    });
}

function makeButton(url, sizeLabel) {
    var $btn = $('<button />')
        .css({ margin: '10px auto 0 auto', display: 'block', width: '100%'})
        .attr('class', 'sc-button sc-button-medium sc-button-responsive')
        .text('Download ' + sizeLabel);

    $btn.on('click', function(e) {
        e.preventDefault();
        download(url);
    });

    return $btn;
}

function download(url) {
    url = url.split('?')[0];
    if (!url.startsWith('http')) {
        url = window.location.protocol + (url.startsWith('//') ? '' : '//') + url;
    }

    var fileNameParts = url.split('/');
    var fileName = fileNameParts[fileNameParts.length - 1];

    var options = {
        url: url,
        name: fileName,
        onerror: function (e) {
            logError('Download failed. Reason: ' + e.error);
        }
    };

    GM_download(options);
}

function logError(message) {
    var details = {
        title: GM_info.script.name,
        text: message,
    };

    GM_notification(details);
    console.error('%c' + GM_info.script.name + '%c: ' + message, 'font-weight: bold', 'font-weight: normal');
}

}, 250);

}

question from:https://stackoverflow.com/questions/65904050/fetch-javascript-input-output-from-google-sheet

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

setInterval is not actually a function within Google App Script. You'll have to utilize the other built-in features. I typically use Utilities.sleep(timeout) to make this work. You can see more here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...