Swift - Read Google Sheets - swift

I'm working on an app that uses Google Sheets as a database, but I can't figure out how to get Swift to read from google sheets. I've looked through the API website and a few questions on here but I need some help just getting started. So far I have;
private let scopes = [kGTLRAuthScopeSheetsSpreadsheets]
private let service = GTLRSheetsService()
var range = "Form Responses 1!A1:D3"
let spreadsheetId = "p;;;;;;p;;;;;;;;1LqXa6v75JE8RQQDOI4Z_g8mUT8x0DhsEDwRIaxDN-DU" // Portfolio
let query = GTLRSheetsQuery_SpreadsheetsValuesGet.query(withSpreadsheetId: spreadsheetId, range:range)
service.executeQuery(query, delegate: self, didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))
Which I got from another question on here...
But 'displayResultWithTicket' is an unresolved identifier. Currently it gets to the last line, and debugging I'm not even sure what to do with the 'query' value. I'm really not even sure if it's reading the correct spreadsheet. How can I tell?
Long story short, I'm lost.

I recently made an app that uses Google Sheets. But I used the CSV file for the database. This might help. If you want, I can share the details.

First you need to export the Google Sheets file in CSV format. I used Realm to read CSV. Using Realm Studio, you need to create the columns as in the CSV file. Then you can import the CSV file into Realm Studio. Your database is ready.
In didFinishLaunchingWithOptions to find the file created after installing Realm on your project
print(Realm.Configuration.defaultConfiguration.fileURL!.path)
Now we will use the database with Swift.
For this, we must first add the Realm file that we prepared to the XCode project directory. Then we use it in ViewController this way to get data.
var realm : Realm!
For get the data in default.realm file
let realmPath = Bundle.main.url(forResource: "default", withExtension: "realm")!
let realmConfiguration = Realm.Configuration(fileURL: realmPath, readOnly: true)
do{
realm = try Realm(configuration: realmConfiguration)
}
catch {
print("error \(error.localizedDescription)")
}
You can export the result to an array. I hope it helps.

Related

Find latest modified file in Folder

I'm trying to find the latest text file in a folder so I can open it. So far I have:
let logFolder = URL(fileURLWithPath: "/Users/me/Library/Logs" )
let fm = FileManager.default
var files = try fm.contentsOfDirectory(at: logFolder, includingPropertiesForKeys: [.creationDateKey], options: [])
let txtFilePaths = files.filter{$0.pathExtension == "txt"}
But then I get stuck. I know I can get the date for a file with txtFilePath[x].creationDate
Seems like there should be a simple way of doing this but I'm a newbie and struggling to find any web resources for Swift 5.
Cheers
Such information about files is stored in something that is usually referred to as metadata.
I believe you are looking for something like NSMetadataItem.
By following the steps presented in this response, you should be able to have access to a field called kMDItemContentModificationDate. This should help you achieve your goal.

How do you retrieve data from Firebase that has been manually entered?

I'm creating a new iOS app for a lacrosse team. I've manually added a registrationCode into my Firebase database.
I've tried getting a reference to the database and then using the .observe method to get the value but it crashes.
My latest code attempt:
let rootRef = Database.database().reference().child("groove-lacrosse")
rootRef.observe(.value, with: {snapshot in
print(snapshot.value as Any)
})
What's the correct way, programmatically, to retrieve Firebase data that was manually entered?
The image is the Firebase entry
It looks like groove-lacrosse is the name of your database, in which case you don't need to specify that anywhere.
Instead you can just read the registration code directly and then print its value:
let rootRef = Database.database().reference().child("registrationCode")
rootRef.observe(.value, with: {snapshot in
print(snapshot.value as Any)
})

Value of type 'MSTable?' has no member 'pullWithQuery'

I tried to change client page size in Azure server
it's default is 50 and I want to make it bigger
so i use Microsoft tutorial in this link
https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-ios-how-to-use-client-library#querying
var client : MSClient?
let client = MSClient(applicationURLString: "AppUrl")
let table = client.tableWithName("TodoItem")
let query = table.query()
let pullSettings = MSPullSettings(pageSize: 3000)
but when I write
table.pullWithQuery(query, queryId:nil, settings: pullSettings) { (error) in
if let err = error {
print("ERROR ", err)
}
}
there are error "Value of type 'MSTable?' has no member 'pullWithQuery'"
what is the problem ?
is the function name changed ?
Two problems:
The documentation has not been updated for current versions of Swift
(an update request has been filed). The correct function name in modern Swift is pull rather than pullWithQuery.
The pullWithQuery function is on MSSyncTable, not MSTable. Pull is part of the offline sync system. The MSTable analog is read.
More details:
The SDK itself defines the function as MSSyncTable.pullWithQuery, but one of the features of Swift 3.0 is that it renames Objective C methods when it projects them into Swift to remove redundant arguments from the name, so verbWithX(X) becomes just verb(with:x) and pullWithQuery (MSQuery) becomes pull(with:MSQuery).
For more information on Swift 3 changes please see https://swift.org/blog/swift-3-0-released/ . I believe this particular change is SE-0005: Better Translation of Objective-C APIs Into Swift
If you download the Swift quickstart from your Azure Portal then you’ll get the correct modern pattern there:
self.table!.pull(with: self.table?.query(), queryId: "AllRecords")
or with your arguments:
self.table!.pull(with: self.table?.query(), queryId: nil, settings: pullSettings)

Swift CSVImporter Cannot use instance member path within property

I am currently trying to import a CSV file using CSVImporter. The github repo says that it is up to date with the current Swift version 3.0. However I am getting an error which I couldn't find a solution to on the internet.
This is the error: http://imgur.com/imhSoQi
This is the github repo: https://github.com/Flinesoft/CSVImporter
I would gladly provide anymore information if needed.
These errors are unrelated to the CSVImporter library.
The first error happens because you are referring one ivar from another. Essentially this is not valid in Swift:
class ABC {
let hello = "Hello"
let hello_world = hello + " World" // references 'hello'
}
The second error happens because you directly put code into your class. Code in Swift lives in methods or in initialisers, but not directly in a class.
I think this may be what you want:
class ViewController {
let path = "/tmp/blub.csv"
func load() {
let importer = CSVImporter<[String]>(path: path)
importer.startImportingRecords ... {
...
}
}
}
And if this is part of a VC (e.g. you might want to display the CSV in a tableview), you probably want to load the data off the main queue, not in init().
P.S.: Do not only send image links to source code. Copy/paste the relevant source code into the question.

trying to access Thunderbird-tabmail does not work

I want to open a new tab with a gloda conversation from inside calendar code.
I receive an error from error console:
window not defined (or document not defined), depending on which of the two I use to Access tabmail:
let tabmail = window.document.getElementById("tabmail");
let tabmail = document.getElementById("tabmail");
The code works fine if the js file is included in an overlay xul-file.
But I want to use it outside of xul in my code.
Somewhere in my calendar code (in my 'addevent'), the same code throws the error.
This code is originally called from a rightclick on an email, but several layers deep into calendar code.
In MDN, I read that window is global? So what do I Need to do to add an tab?
This part works if tabmail is properly referenced:
tabmail.openTab("glodaList", {
collection: queryCollection,
message: aCollection.items[0],
title: tabTitle,
background: false
});
So how do I get a reference for tabmail?
Any help is appreciated.
after trying and looking through code for really some time before posting, it took only ca. 20 minutes to accidentally find the solution after submitting the question..
While browsing mailutils on mxr for something else, I found the solution in some function:
mail3PaneWindow = Services.wm.getMostRecentWindow("mail:3pane");
if (mail3PaneWindow) var tabmail = mail3PaneWindow.document.getElementById("tabmail");