I'm trying to link a spreadsheet to a document. I've put the spreadsheet's id in the header of the document - (getChild(3) because I'm using 'different first page header'). In the log I get the right id value, but the script return "An unexpected error" for openById...
function spreadSheet(){
var docFile = DocumentApp.getActiveDocument();
var header = docFile.getHeader();
var id = header.getParent().getChild(3).asHeaderSection().getText();
//var id = header.getParent().getChild(3).asText().getText()
//var id = '1cYX1uXHQdB0ee67YWLanB_ESTgGkG9NRJIQ34arIH2c'
Logger.log(id)
var ss = SpreadsheetApp.openById(id);
var s = ss.getSheetByName('Data');
var hej = s.getRange('A1').getValue();
I've tried some different stuff as you can see in the code. When replacing the id with the text string in the last example it works as supposed. But I wan't the id to be defines in the document and not in the code.
I think that the id will be the longest word in the header text so I'd guess that this will work.
function getSSIDFromHeader(){
var docFile = DocumentApp.getActiveDocument();
var header = docFile.getHeader();
var text=header.getText().split(/(\n| |\r)/);//splitting on cr linefeed or space
DocumentApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(text.reduce(function(a,s,i){if(s.length>a.max.length){a.max=s;}return a;},{max:''}).max),"Test");
}
I tested this on a document header of mine and it works. I suspect that it might on yours because I guess that it would be the longest string in the header when split by either \r or \n or space