Search code examples
excelswiftmacosnspasteboard

Write data to NSPasteboard and paste into Excel split by column and line


Regarding use of NSPasteboard by Swift. I want to write an OS X program to copy NSTableView data and paste it into Excel split by row and column, to match Excel format. Current code:

@IBAction func copyDataButton(sender: AnyObject) {

    let pasteBoard = NSPasteboard.generalPasteboard()

    pasteBoard.clearContents()
    pasteBoard.writeObjects(["data to excel"])


}

I don't know how to adjust NSPasteBoardWriting data in NSPasteboard to insert a ASCII or ???


Solution

  • I had find the answer, you can construct the string use \t for tab a column, use \n to change line.

    let tsvString = "data to excel\n then change line \t then tab a column"
    
    let pasteBoard = NSPasteboard.general()
    
    pasteBoard.clearContents()
    pasteBoard.writeObjects([tsvString as NSString])