Skip to content

Instantly share code, notes, and snippets.

@donnaken15
Last active April 22, 2021 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donnaken15/cc3f5a4370f46225a0bb41b0e95e448f to your computer and use it in GitHub Desktop.
Save donnaken15/cc3f5a4370f46225a0bb41b0e95e448f to your computer and use it in GitHub Desktop.
Generate subtitles file in Drive from current (Excel) spreadsheet, formatted like https://docs.google.com/spreadsheets/d/1nzDgRlGuB6SY2cvfsJLH7u3_G0Ouucm-l8xsCzmfukw/edit?usp=sharing
function SRTstamp(_time) {
if (_time !== undefined)
{
if (_time.lastIndexOf(":") < 4)
_time = "00:"+_time
_time = _time.replace(".",",")+"0"
return _time
}
else throw("Invalid argument")
}
function genSubs() {
var final = ""
var capnum = 0
var subs = SpreadsheetApp.getActiveSheet();
cont = subs.getDataRange().getValues();
cont.forEach(function (row) {
if (capnum > 0)
final += capnum.toString()+"\n"+(SRTstamp(row[0])+" --> "+SRTstamp(row[1]))+"\n"+row[2]+"\n\n";
capnum++;
});
final += "\n"
var newF = DriveApp.createFile("Captiongen"+Date.now()+"_"+subs.getSheetName()+".srt".toString(),final);
SpreadsheetApp.getUi().alert("File created at:\n" + newF.getUrl());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment