2

I'm trying to create a .txt file using the excel javascript API and I have no idea how to get this done (I'm new to coding)

The text I want to be inside the .txt file is already in a variable named "output".

This is my try:

    function saveTxt(txt) {     
  var name = context.workbook.name
  var Path = Office.context.document.url.slice(0,-name.length)
  var saveFile = new File(Path + "data.txt");

  if (saveFile.exists){
    saveFile.remove();
  }

  saveFile.encoding = "UTF8";
  saveFile.open("e", "TEXT", "????");
  saveFile.writeln(txt);
  saveFile.close();      
}

saveTxt(output)

When I run the code I get a "function expected" error. Would be nice to count with your help to solve that!

Btw, I don't know if it's relevant, but I'm coding directly in the ScriptLab excel add-in

Thanks!

David
  • 21
  • 1
  • To be clear this is a `.js` server side file? – Michael Nelles Mar 20 '20 at 00:36
  • Which line is throwing the "function expected"? Also, check out this SO question: https://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript – Rick Kirkham Mar 20 '20 at 06:48
  • There is no server involved. Everything is local. Is a script I run in excel using the ScriptLab add-in. It's like creating a Macro, but instead of VBA I'm using JS. The line throwing the "function expected" is the "var saveFile = new File(Path + "data.txt");" – David Mar 20 '20 at 09:48
  • 1
    Where do you see a File object that takes a string as its only parameter? The one defined here does not: https://developer.mozilla.org/en-US/docs/Web/API/File/File – Rick Kirkham Mar 20 '20 at 18:28

0 Answers0