Pergunta

Cannot find method open().

  1. If it can't find it, why does it let me choose it when I type the dot?
  2. So how am I supposed to open a file? openByID()? Really? Where do I get this magical ID? From getURL? Seriously? This is the only thing that comes up:

    SpreadsheetApp.openById(id).getUrl()
    

    but I fail to understand how I'm supposed to get the URL if I don't have an ID to hang it on... leading to a catch 22. Further, I presume in the event I do get the URL I still need to parse it to get the ID... and what magical method have they supplied for that particular incantation?

Clearly, my understanding is lacking. Any help?

function copyTemplate() {
  var targetSpreadsheetName="The file";

  var targetSpreadsheetID=SpreadsheetApp.open(targetSpreadsheetName).getId();
  targetSpreadsheetID.insertSheet("CellData", 1, {template:temp});


  // The code below will duplicate the sheet named "CellData" and insert it after 
  // the 1st sheet and call it "CellData"
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var temp = ss.getSheetByName("CellData");
  ss.insertSheet("CellData", 1, {template:temp});
}

The question is multi-part.

The error message returned was "Cannot find method open()."

The first question is "Why can't it find method open()?".

The second question is "If in fact, it can't find it, why does it let me choose it when I type the dot?"

The third question is "Considering the fact that the open() method fails to work, how am I supposed to open a file?" The presumed answer I expect to receive from the community is "with openByID()".

That leads to the fourth question which is "Where do I get the ID?". The apparent answer seems to be "getURL()".

And that leads to the fifth question which is "How do I use getURL()?" getURL seems to require an ID. If getURL requires an ID to get the URL, and openByID requires a URL to get the ID, you have an infinite loop. Surely I'm misunderstanding something.

The sixth question is "In the event getURL() ends up being part of the solution, how does one distinguish the ID from the rest of the string returned?"

I hope that clarifies my question.

Foi útil?

Solução

Well I'll finally try to answer your question, hoping you'll find it clear enough... ;-)

First thing first :

var ss = SpreadsheetApp.open()

does indeed appear in the autocomplete after the dot, what you didn't pay attention to is that the argument is a file, ie an object returned by an appropriate statement. Now let's see how to get it and , with this example how the other parameters of this file can be obtained :

    function myFunction() {
    var files = DocsList.find('thisisthesheetIwant');// this is an array of file objects that include the term 'thisisthesheetIwant'
    var file = files[0];// I take the first one
    var filename = file.getName();//and get its name
    var fileId = file.getId();// its ID
    var fileurl = file.getUrl();//and its url
// then I show the results in the logger
    Logger.log('number of file found = '+files.length+'\n\n'+filename+'\n\n'+fileId+'\n\n'+fileurl+'\n\n')
    var ss = SpreadsheetApp.open(file);// using that file object I can open a spreadsheet
    var content = ss.getSheets()[0].getDataRange().getValues().toString();// and get the whole content of the first sheet
    Logger.log('content = '+content);// there it is
    }

The spreadsheet with its code is available here so you can test it by yourself, I named it thisisthesheetIwant hoping you don't have any file with a similar name or content since it wouldn't work as I expected if more than 1 file was returned.

Look at the logger and I hope it will answer your question(s). it should appear like this below : enter image description here

And the sheet itself is like this :

enter image description here

EDIT : note that the ID and the URL have a common part, the url is what you can see in your Browser's adress bar, the ID is just a part of it. Now you can open the same spreadsheet with

SpreadsheetApp.openById(fileId)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top