来自 lichess.org,假设 pgn 文件已加载到您的驱动器中(如果我直接从 lichess.org 捕获信息,谷歌警报“超过最大执行时间”)
// Mike Steelson
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('↓ lichess.org ↓')
.addItem('parse PGN file from Drive', 'readPGNFromDrive')
.addToUi();
}
function readPGNFromDrive() {
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('from drive')
var id = sh.getRange('B1').getValue()
var list = ['Event', 'Site', 'Date', 'White', 'Black', 'Result', 'UTCDate', 'UTCTime', 'WhiteElo', 'BlackElo', 'WhiteRatingDiff', 'BlackRatingDiff', 'Variant', 'TimeControl', 'ECO', 'Termination', 'FEN', 'SetUp', 'sequence']
sh.deleteRows(2,sh.getLastRow()+2)
var result=[]
result.push(list)
var data = DriveApp.getFileById(id).getBlob().getDataAsString().split(String.fromCharCode(10,10,10))
for (var i=0; i<data.length; i++) {
var prov=[]
var parts = data[i].split(String.fromCharCode(10,10))
var pparts = parts[0].split(String.fromCharCode(10))
for (var x=0; x<pparts.length; x++){
pparts[x]=pparts[x].replace('[','"').replace(']','').replace(' "','":"') // "key":"value"
}
var donnees = JSON.parse('{'+pparts.join(',')+'}') // mise au format json
list.forEach(function(path){
if (path == 'sequence'){prov.push(parts[1])}
else {prov.push(donnees.item(path))}
})
result.push(prov)
}
sh.getRange(2,1,result.length,result[0].length).setValues(result)
}
Object.prototype.item=function(i){return this[i]};
https://docs.google.com/spreadsheets/d/1ZX-YefSfeNx0z6mo7Bo3EY_tOkYcVLT7Ke0fqQUiHNI/copy