Obtain Google calnedar information using a for statement based on data read from CSV

Asked 2 years ago, Updated 2 years ago, 116 views

I'm a beginner at js.This may be a rudimentary question, but please forgive me.

Read the Google Calendar ID information stored in the CSV and
From there, I thought about looping using for minutes to retrieve information from each calendar.

I don't know why, but I can retrieve the calendar information, but
The identity information and rank information that accompanies it from the CSV are not reflected well.

I looked at the form of using callback function and debuffer in function.
I didn't get it right.

Thank you for your efforts.

problems:

In fact, only irank variables behave strangely, and data.items[0].summary is fine.
→ CSV last line string (irank=csvListall[263][5]=b) is substituted by all irank.

Results requested this time:

a+ string (when data.items[0].summary result ← i = 0)
b+ string (when data.items[0].summary result ← i = 1 )
ss+ string (when data.items[0].summary result ← i = 2 )
c+ string (when data.items[0].summary result ← i = 3 )
b+ string (when data.items[0].summary result ← i = 4)
ss+ string (when data.items[0].summary result ← i = 5)
s+ string (when data.items[0].summary result ← i = 6)
↓
Continuing...

Results:

b+ string (when data.items[0].summary result ← i = 0)
b+ string (when data.items[0].summary result ← i = 1 )
b+ string (when data.items[0].summary result ← i = 2 )
b+ string (data.items[0].summary result ← i = 3 )
b+ string (when data.items[0].summary result ← i = 4)
b+ string (when data.items[0].summary result ← i = 5)
b+ string (when data.items[0].summary result ← i = 6)
↓
Continuing...
functionKansu(){

// Tokens required when using the GOOGL CALEMDAR API
    var apikey = 'Personal API Key';

// Process Date Relationships in GOOGL CALEMDAR
    var now = new Date();
    variable=now.getFullYear();
    varm = now.getMonth()+1;
    vard = now.getDate();
    varw = now.getDay();
    varwd=["Sun", "Mon", "Tuesday", "Wednesday", "Thursday", "Friday", "Sat";
    varh = now.getHours();
    varmi = now.getMinutes();
    vars = now.getSeconds();
    varmm=("0"+m).slice(-2);
    vardd=("0"+d).slice(-2);
    var hh=("0"+h).slices(-2);
    varmmi=("0"+mi).slice(-2);
    vars=("0"+s).slice(-2);

// GOOGL CALEMDAR Tomorrow Date
    vardmax = now.getDate()+1;
    var ddmax=("0"+dmax).slices(-2);

// GOOGL CALEMDAR Today - Tomorrow Date
    var timeMin=y+"-"+mm+"-"+dd+"T"+hh+":"+mmi+":"+ss+"Z";
    var timeMax=y+"-"+mmmax+"-"+dd+"T"+hh+":"+mmi+":"+ss+"Z";



// First get the Google Calendar address from the CSV on the server
    $.ajax({
        url: 'List Google Calendar addresses .csv',
        success:function(era){
        csvListall=$.csv()(era);

// Check Google Calendar addresses one line at a time per total (
    for(vari=0;i<264;i++){
    var calendarId=';
    varcalendarId=csvListall[i][2];

// Obtain rank information (sss,ss,s,s,a,b,c) for each Google Calendar address you get
    varirank=';
    variablerank=csvListall[i][5];



// Get information from Google Calendar at $.getJSON
        varuri="https://www.googleapis.com/calendar/v3/calendars/"+calendarId+"/events?key="+apikey+"&timeMin="+timeMin+"&timeMax="+timeMax+"&maxResults=10&orderBy=startTime&singleEvents=true";
        varjsinfo=uri;
            $.getJSON(jsinfo,
            function(data){
                // Run when retrieved summary is not empty
                if(data.items[0].summary!=""){
                // Replace #images with string
                    $("#images").append(irank+data.items[0].summary);
                         }
                    });
                }
            }
    });
}

Enter a description of the image here

javascript json

2022-09-30 20:16

2 Answers

The irank referenced by the callback function function(data) in $.getJSON is defined by another function function(era).Therefore, one irank state is reserved for each function(era) run.
Meanwhile, $.getJSON invokes callback calls asynchronously.Therefore, after $.getJSON is called 264 times in function(era), function(data) is executed 264 times, and all irank values are in the 264th state.

Rewrite the function(data) as follows, for example, to save the irank for each call.

(function(irank){
    return function(data){
        // …
    };
}) (irank)


2022-09-30 20:16

The cause is exactly what pgrho wrote.

One JavaScript trap that you will definitely fall for someday
http://qiita.com/ukiuni @github/items/463493a690265cec8bb7

There are also jQuery.each() and Array.prototype.forEach() arrays.

Array.prototype.slice(0,264) should only process 264 pieces from the first element, but if you want to process all csvListall elements, delete this method as it is not necessary.

jQuery.each():

$.ajax({
    url: 'List Google Calendar addresses .csv',
    success:function(era){
        csvListall=$.csv()(era);

        // Check Google Calendar addresses one line at a time per total
        $.each(csvListall.slice(0,264), function(index,value){
            var calendarId = value[2];

            // Obtain rank information (sss,ss,s,s,a,b,c) for each Google Calendar address you get
            variablerank=value[5];

            //...

        });
    }
});

Array.prototype.forEach():

$.ajax({
    url: 'List Google Calendar addresses .csv',
    success:function(era){
        csvListall=$.csv()(era);

        // Check Google Calendar addresses one line at a time per total
        csvListall
            .slice(0,264)
            .forEach(function(value,index){
                var calendarId = value[2];

                // Obtain rank information (sss,ss,s,s,a,b,c) for each Google Calendar address you get
                variablerank=value[5];

                //...

            });
    }
});


2022-09-30 20:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.