Insert a Table into a Word Document in Swift
Once you’ve generated an editing URL for an online Word document with our Begin Editing function, you’re ready to perform editing operations on the file. This brief article will show how you can use an API in Swift to add a table to a DOCX file. The operation allows you to specify how the table is set up through parameters such as font size and color, border and cell shading, cell width, and more.
To call the function, you will input your editing URL and API key, along with any specifications for your input request, into the following code:
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endifvar semaphore = DispatchSemaphore (value: 0)let parameters = "InputFileBytes=%3Cbyte%3E&InputFileUrl=%3Cstring%3E&TableID=reprehenderit%20fugiat%20Excepteur%20non%20irure&Path=voluptate%20ad%20nisi%20dolor&Width=mollit%20ad&WidthType=nostrud%20cupidatat%20Ut&TableRows=%5B%7B%22RowIndex%22%3A66021612%2C%22Path%22%3A%22sunt%20tempor%22%2C%22RowCells%22%3A%5B%7B%22CellIndex%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22Path%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22Paragraphs%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingColor%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingFill%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingPattern%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellWidthMode%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellWidth%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%7D%2C%7B%22CellIndex%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22Path%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22Paragraphs%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingColor%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingFill%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingPattern%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellWidthMode%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellWidth%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%7D%5D%7D%2C%7B%22RowIndex%22%3A-52618426%2C%22Path%22%3A%22laborum%20ex%20nulla%22%2C%22RowCells%22%3A%5B%7B%22CellIndex%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22Path%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22Paragraphs%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingColor%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingFill%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingPattern%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellWidthMode%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellWidth%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%7D%2C%7B%22CellIndex%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22Path%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22Paragraphs%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingColor%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingFill%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellShadingPattern%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellWidthMode%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%2C%22CellWidth%22%3A%7B%22value%22%3A%22%3CError%3A%20Too%20many%20levels%20of%20nesting%20to%20fake%20this%20schema%3E%22%7D%7D%5D%7D%5D&TopBorderType=dolor%20anim%20consectetur%20id%20do&TopBorderSize=-48699561&TopBorderSpace=39428009&TopBorderColor=dolore%20aute&BottomBorderType=cillum%20E&BottomBorderSize=-25468925&BottomBorderSpace=-54605955&BottomBorderColor=est%20anim%20labore%20dolor%20Ut&LeftBorderType=dolore%20aute%20do%20officia%20sit&LeftBorderSize=-32092416&LeftBorderSpace=51220927&LeftBorderColor=exercitation%20qui%20Duis%20reprehenderit%20Lorem&RightBorderType=elit%20dolore&RightBorderSize=-98164652&RightBorderSpace=59012741&RightBorderColor=labore%20amet%20id%20sit%20ex&CellHorizontalBorderType=n&CellHorizontalBorderSize=38762590&CellHorizontalBorderSpace=-83261608&CellHorizontalBorderColor=mollit%20ipsum%20qui%20exercitati&CellVerticalBorderType=id%20pariatur%20et%20nostrud%20tempor&CellVerticalBorderSize=81600348&CellVerticalBorderSpace=88089454&CellVerticalBorderColor=nulla%20cupidatat&StartBorderType=consectetur%20nisi%20tempor%20velit%20laborum&StartBorderSize=-5561060&StartBorderSpace=74403043&StartBorderColor=magna%20esse&EndBorderType=aliqua%20mollit%20ad%20in%20cupidatat&EndBorderSize=-91030522&EndBorderSpace=33016279&EndBorderColor=nostrud&TableIndentationMode=incididunt%20pariatur%20voluptate%20et&TableIndentationWidth=84550725&InsertPlacement=%3Cstring%3E&InsertPath=%3Cstring%3E"
let postData = parameters.data(using: .utf8)var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/convert/edit/docx/insert-table")!,timeoutInterval: Double.infinity)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")request.httpMethod = "POST"
request.httpBody = postDatalet task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}task.resume()
semaphore.wait()
The editing URL will be updated with your changes, and if you have no other edits to perform, you can use the Finish Editing function to close out the process and download the results. If you need to retrieve an API key, head over to the Cloudmersive website to register for a free account; this provides 800 calls/month across our multitude of APIs.