Skip to content

Commit

Permalink
Fix isBibliographyExist err-ed when table dont have any cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Wikidepia committed May 5, 2024
1 parent d5b3c52 commit fa602e5
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ function insertBibliography(createNew: boolean = true) {
var body = baseDoc.getBody();

var markerLinks = [] as string[];
var bibtexIDs = [];
var bibtexIDs = [] as string[][];
var cites = [] as string[];
var citesSearch = body.findText(`​`);
while (citesSearch != null) {
var element = citesSearch.getElement();
var link = element.asText().getLinkUrl(citesSearch.getStartOffset());
markerLinks.push(link);
if (link && link.includes("#cite-mendeley")) {
var bibtexIDLink = [];
var bibtexIDLink = [] as string[];
var documentIDs =
link.split("#cite-mendeley+")?.[1]?.split("+")?.[0]?.split("|") || [];

Expand Down Expand Up @@ -359,16 +359,7 @@ function insertBibliography(createNew: boolean = true) {
}

// Check if table already exists
var allTables = body.getTables();
var table = undefined as GoogleAppsScript.Document.Table | undefined;
for (var i = 0; i < allTables.length; i++) {
var tableText =
allTables[i]?.getCell(0, 0).editAsText().getLinkUrl(0) || "";
if (tableText.includes("#bibs-mendeley")) {
table = allTables[i];
break;
}
}
var table = isBibliographyExist();

// Create new table if clicked from menu bar
if (!table && createNew) {
Expand Down Expand Up @@ -476,14 +467,18 @@ function insertBibliography(createNew: boolean = true) {
}
}

function isBibliographyExist(): boolean {
function isBibliographyExist(): GoogleAppsScript.Document.Table | false {
var body = DocumentApp.getActiveDocument().getBody();
var allTables = body.getTables();
for (var i = 0; i < allTables.length; i++) {
var tableText =
allTables[i]?.getCell(0, 0).editAsText().getLinkUrl(0) || "";
var table = allTables[i];
try {
var tableText = table.getCell(0, 0).editAsText().getLinkUrl(0) || "";
} catch (e) {
continue;
}
if (tableText.includes("#bibs-mendeley")) {
return true;
return table;
}
}
return false;
Expand Down

0 comments on commit fa602e5

Please sign in to comment.