Skip to content

Instantly share code, notes, and snippets.

@mcancellieri
Last active August 29, 2015 13:59
Show Gist options
  • Save mcancellieri/10736099 to your computer and use it in GitHub Desktop.
Save mcancellieri/10736099 to your computer and use it in GitHub Desktop.
Create a traceability matrix in a spreadsheet with Google docs.
/**
* Instructions:
* WARNING: all the titles you want to collect should have a common and unique style!
* 1) Go to your first document.
* 2) Open your second document in a new tab.
* 3) On the first document go to Tools->Scripts
* 4) Paste this code on a blank project
* 5) Properly fill the variables below.
* 6) Hit Run onOpen() to add a menu voice
* 7) Hit REQ->'Create traceability matrix' to run it.
*
*/
var SPREADSHEETNAME = "TraceabilityMatrix";
var UCSFILEURL = "YOUR URL HERE";
var HEADINGTYPE = DocumentApp.ParagraphHeading.HEADING3;
var PACKAGE_PREFIX = "P";
var UCS_PREFIX = "UCS";
function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
DocumentApp.getUi().createMenu('REQ')
.addItem('Create traceability matrix', 'traceabilityMatrix')
.addToUi();
}
/**
* Get all the HEADINGTYPE titles and put them on a new spreadsheet
*/
function traceabilityMatrix() {
var now = new Date();
var timestamp = "" + now.getFullYear() + (now.getMonth()+1) + now.getDay() + now.getHours() + now.getMinutes() + now.getSeconds();
var newDoc = SpreadsheetApp.create(SPREADSHEETNAME + "_" + timestamp);
var paragraphs = DocumentApp.getActiveDocument().getBody().getParagraphs();
var body = DocumentApp.getActiveDocument().getBody();
var packages = new Array();
packages.push("");
for (var i=0; i < paragraphs.length; i++){
if (paragraphs[i].getHeading()==HEADINGTYPE && paragraphs[i].getText().lastIndexOf(PACKAGE_PREFIX, 0)===0){
packages.push(paragraphs[i].getText());
}
}
newDoc.getSheets()[0].appendRow(packages);
var bodyUCS = DocumentApp.openByUrl(UCSFILEURL).getBody();
var paragraphsUCS = bodyUCS.getParagraphs();
var ucses = new Array();
for (var i=0; i < paragraphsUCS.length; i++){
if (paragraphsUCS[i].getHeading()==HEADINGTYPE && paragraphsUCS[i].getText().lastIndexOf(UCS_PREFIX, 0)===0){
ucses.push(paragraphsUCS[i].getText());
var row = new Array();
for (var k=0; k<packages.length; k++){
row.push("");
}
row[0]=paragraphsUCS[i].getText();
newDoc.getSheets()[0].appendRow(row);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment