function crossref_agency (DOI) {

var http_response = UrlFetchApp.fetch ('https://api.crossref.org/works/' + (DOI) + '/agency')
var api_data = http_response.getContentText();
var json = JSON.parse(api_data);
var agency = json["message"]["agency"]["label"];

var results = [[agency]]

return results;

}


function crossref_publisher (DOI) {

var http_response = UrlFetchApp.fetch ('https://api.crossref.org/works/' + (DOI))
var api_data = http_response.getContentText();
var json = JSON.parse(api_data);
var member = json["message"]["member"];

var http_response2 = UrlFetchApp.fetch ('https://api.crossref.org/members/' + member)
var api_data2 = http_response2.getContentText();
var json2 = JSON.parse(api_data2);
var publisher = json2["message"]["primary-name"];

var results = [[member, publisher]]

return results

}


function crossref_generic (DOI) {

var http_response = UrlFetchApp.fetch ('https://api.crossref.org/works/' + (DOI))
var api_data = http_response.getContentText();
var json = JSON.parse(api_data);
var type = json["message"]["type"];
var issued_array = json["message"]["issued"]["date-parts"];

// below is a ternary operator, it asks "is issued_array set?
// if it is, set issued_year to its first element, otherwise set it to NaN.
// it's a neat short hand for an if/else statement and will get rid of #ERROR results

var issued_year = issued_array ? issued_array[0][0] : NaN;

var results = [[type, issued_year]]

return results;

}


function crossref_funder_names (DOI) {

var http_response = UrlFetchApp.fetch ('https://api.crossref.org/works/' + (DOI))
var api_data = http_response.getContentText();
var json = JSON.parse(api_data);
var funder_array = json["message"]["funder"];

// this is the ternary operator, it asks "is funder_array set? if it is, set fundercount to its length,
// otherwise set it to 0. it's a neat short hand for an if/else statement
// this will get rid of all your #ERROR!s in the sheet whenever there is no funder array in the data,
// and replace them with 0s

var fundercount = funder_array ? funder_array.length : 0;

// we want to make a row which has the funder count, followed by however many funders there are.
// So we can start building that row straight away with the fundercount we calculated above

var result = [fundercount];

// now we just go through the funder list (using fundercount as our loop limit, as that's always a number
// 0 or greater and for each funder we use the array push function to add the funder name to our result array
// created above

for(let i = 0; i < fundercount; i++) {
result.push(json.message.funder[i].name)
}

// we can wrap the single row result in an array as we return it, since the sheet is expecting a 2D array

return [result];

}


function crossref_fundref (DOI) {

var http_response = UrlFetchApp.fetch ('https://api.crossref.org/works/' + (DOI))
var api_data = http_response.getContentText();
var json = JSON.parse(api_data);
var funder_array = json["message"]["funder"];

// this is the ternary operator, it asks "is funder_array set? if it is, set fundercount to its length,
// otherwise set it to 0. it's a neat short hand for an if/else statement
// this will get rid of all your #ERROR!s in the sheet whenever there is no funder array in the data,
// and replace them with 0s

var fundercount = funder_array ? funder_array.length : 0;

var myList = ["10.13039/501100003246", "10.13039/501100010409","10.13039/501100003958", "10.13039/501100010071"];

for(let i = 0; i < fundercount; i++) {
var funder_doi = json.message.funder[i].DOI
var funder_doi_asserted = json.message.funder[i]["doi-asserted-by"]

if (myList.includes(funder_doi)) break
}

var funder_nwo = myList.includes(funder_doi) ? true : false ;
var funder_nwo_asserted = (funder_nwo) ? funder_doi_asserted : "" ;

///if (myList.includes(funder_doi)) {
/// var funder_nwo = "Ja"
///}

var results = [[funder_nwo, funder_nwo_asserted]]

return results;

}


function crossref_agency_json (DOI) {

var http_response = UrlFetchApp.fetch ('https://api.crossref.org/works/' + (DOI) + '/agency')
var api_data = http_response.getContentText();
var json = JSON.parse(api_data);
var agency = json["message"]["agency"]["label"];

var results = [[http_response, api_data, json, agency]]

return results;

}


function dimensions_json (DIM) {

var api_data = DIM;
var json = JSON.parse(api_data);
var funder_array = json; //this is is just renaming for consistency

//calculate number of funders
var fundercount = funder_array ? funder_array.length : 0;

//check if funders include NWO
var myList = ["NWO"];

for(let i = 0; i < fundercount; i++) {
var funder_acronym = funder_array[i].acronym

if (myList.includes(funder_acronym)) break
}

var funder_nwo = myList.includes(funder_acronym) ? true : false ;

//extract funder acronyms as string
var funder_acronyms = []
for(let i = 0; i < fundercount; i++) {
funder_acronyms.push(funder_array[i].acronym)
}

var funder_acronyms_string = funder_acronyms.join()


//extract funder names as string
var funder_names = []
for(let i = 0; i < fundercount; i++) {
funder_names.push(funder_array[i].name)
}

var funder_names_string = funder_names.join()


var results = [[fundercount, funder_nwo, funder_acronyms_string, funder_names_string]]

return results;

}