Csv format (not xml) modified to Excel default

remove unnecessary " and use ; as separator
This commit is contained in:
Philoul 2021-03-07 10:43:33 +01:00
parent 6096825f4c
commit c259aa345c
8 changed files with 32 additions and 58 deletions

View file

@ -364,7 +364,7 @@ class ImportExportPrefs @Inject constructor(
log.debug("XXXXX " + entries.size) log.debug("XXXXX " + entries.size)
prefFileList.ensureExportDirExists() prefFileList.ensureExportDirExists()
val newFile = prefFileList.newExportXmlFile() val newFile = prefFileList.newExportXmlFile()
log.debug("XXXXX " + classicPrefsFormat.returnXml(entries)) log.debug("XXXXX " + classicPrefsFormat.UserEntriesToCsv(entries))
askToConfirmExport(activity, newFile) { password -> askToConfirmExport(activity, newFile) { password ->
try { try {

View file

@ -18,7 +18,6 @@ import info.nightscout.androidaps.interfaces.ImportExportPrefsInterface
import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.logging.UserEntryLogger import info.nightscout.androidaps.logging.UserEntryLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.nsclient.events.EventNSClientRestart
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.DecimalFormatter import info.nightscout.androidaps.utils.DecimalFormatter
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
@ -61,7 +60,7 @@ class TreatmentsUserEntryFragment : DaggerFragment() {
binding.recyclerview.layoutManager = LinearLayoutManager(view.context) binding.recyclerview.layoutManager = LinearLayoutManager(view.context)
binding.ueExportToXml.setOnClickListener { binding.ueExportToXml.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_xml) + "?") { OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.ue_export_to_csv) + "?") {
uel.log(Action.TREATMENTS_NS_REFRESH) uel.log(Action.TREATMENTS_NS_REFRESH)
importExportPrefs.exportUserEntriesXml(activity, repository.getAllUserEntries()) importExportPrefs.exportUserEntriesXml(activity, repository.getAllUserEntries())
} }

View file

@ -12,7 +12,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:drawableStart="@drawable/ic_header_export" android:drawableStart="@drawable/ic_header_export"
android:text="@string/ue_export_to_xml" /> android:text="@string/ue_export_to_csv" />
<androidx.recyclerview.widget.RecyclerView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview" android:id="@+id/recyclerview"

View file

@ -31,7 +31,7 @@ class PrefFileListProvider @Inject constructor(
private val path = File(Environment.getExternalStorageDirectory().toString()) private val path = File(Environment.getExternalStorageDirectory().toString())
private val aapsPath = File(path, "AAPS" + File.separator + "preferences") private val aapsPath = File(path, "AAPS" + File.separator + "preferences")
private val xmlPath = File(path, "AAPS" + File.separator + "xml") private val exportsPath = File(path, "AAPS" + File.separator + "exports")
private const val IMPORT_AGE_NOT_YET_OLD_DAYS = 60 private const val IMPORT_AGE_NOT_YET_OLD_DAYS = 60
} }
@ -94,8 +94,8 @@ class PrefFileListProvider @Inject constructor(
if (!aapsPath.exists()) { if (!aapsPath.exists()) {
aapsPath.mkdirs() aapsPath.mkdirs()
} }
if (!xmlPath.exists()) { if (!exportsPath.exists()) {
xmlPath.mkdirs() exportsPath.mkdirs()
} }
} }
@ -106,7 +106,7 @@ class PrefFileListProvider @Inject constructor(
fun newExportXmlFile(): File { fun newExportXmlFile(): File {
val timeLocal = LocalDateTime.now().toString(DateTimeFormat.forPattern("yyyy-MM-dd'_'HHmmss")) val timeLocal = LocalDateTime.now().toString(DateTimeFormat.forPattern("yyyy-MM-dd'_'HHmmss"))
return File(xmlPath, timeLocal + "_UserEntry.xml") return File(exportsPath, timeLocal + "_UserEntry.csv")
} }
// check metadata for known issues, change their status and add info with explanations // check metadata for known issues, change their status and add info with explanations

View file

@ -79,20 +79,7 @@ class ClassicPrefsFormat @Inject constructor(
fun saveXml(file: File, userEntries: List<UserEntry>) { fun saveXml(file: File, userEntries: List<UserEntry>) {
try { try {
val contents = userEntries.joinToString("\n") { entry -> val contents = UserEntriesToCsv(userEntries)
if (entry.values.size > 0) {
dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "," +
dateUtil.dateAndTimeAndSecondsString(entry.utcOffset) + "," +
resourceHelper.gs(entry.action.stringId()) + "," +
"\""+ entry.s.replace("\"", "\\\"") + "\"," +
entry.values.joinToString(",") { value -> valueWithUnitToStringXml(value) } + "\n"
} else {
dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "," +
dateUtil.dateAndTimeAndSecondsString(entry.utcOffset) + "," +
resourceHelper.gs(entry.action.stringId()) + "," +
"\""+ entry.s.replace("\"", "\\\"") + "\",,\n"
}
}
storage.putFileContents(file, contents) storage.putFileContents(file, contents)
} catch (e: FileNotFoundException) { } catch (e: FileNotFoundException) {
throw PrefFileNotFoundError(file.absolutePath) throw PrefFileNotFoundError(file.absolutePath)
@ -101,37 +88,37 @@ class ClassicPrefsFormat @Inject constructor(
} }
} }
fun returnXml(userEntries: List<UserEntry>): String { fun UserEntriesToCsv(userEntries: List<UserEntry>): String {
val userEntryHeader = "Date;UTC Offset;Action;Note;Value;Unit\n"
return userEntries.joinToString("\n") { entry -> return userEntryHeader + userEntries.joinToString("\n") { entry ->
if (entry.values.size > 0) { if (entry.values.size > 0) {
entry.values.joinToString("\n") { value -> entry.values.joinToString("\n") { value ->
"\"" + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "\"," + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + ";" +
"\"" + dateUtil.timeString(entry.utcOffset) + "\"," + dateUtil.timeString(entry.utcOffset) + ";" +
"\"" + resourceHelper.gs(entry.action.stringId()) + "\"," + "\"" + resourceHelper.gs(entry.action.stringId()) + "\";" +
if (entry.s != "") {"\""+ entry.s.replace("\"", "\\\"") + "\"," } else { "," } + if (entry.s != "") {"\""+ entry.s.replace("\"", "\"\"") + "\";" } else { ";" } +
valueWithUnitToStringXml(value) } valueWithUnitToCsv(value) }
} else { } else {
"\"" + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + "\"," + dateUtil.dateAndTimeAndSecondsString(entry.timestamp) + ";" +
"\"" + dateUtil.timeString(entry.utcOffset) + "\"," + dateUtil.timeString(entry.utcOffset) + ";" +
"\"" + resourceHelper.gs(entry.action.stringId()) + "\"," + "\"" + resourceHelper.gs(entry.action.stringId()) + "\";" +
if (entry.s != "") {"\""+ entry.s.replace("\"", "\\\"") + "\"," } else { ",," } if (entry.s != "") {"\""+ entry.s.replace("\"", "\"\"") + "\";" } else { ";;" }
} }
} }
} }
fun valueWithUnitToStringXml(v: ValueWithUnit): String { fun valueWithUnitToCsv(v: ValueWithUnit): String {
return when (v.unit) { return when (v.unit) {
Units.Timestamp -> "\"" + dateUtil.dateAndTimeAndSecondsString(v.lValue) + "\",\"" + resourceHelper.gs(R.string.date) + "\"" Units.Timestamp -> dateUtil.dateAndTimeAndSecondsString(v.lValue) + ";" + resourceHelper.gs(R.string.date)
Units.CPEvent -> "\"" + translator.translate(v.sValue) + "\"," Units.CPEvent -> translator.translate(v.sValue) + ";"
Units.R_String -> "\"" + resourceHelper.gs(v.iValue) + "\"," Units.R_String -> "\"" + resourceHelper.gs(v.iValue).replace("\"", "\"\"") + "\";"
Units.Mg_Dl -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue) + ",\"" + resourceHelper.gs(Units.Mg_Dl.stringId()) + "\"" else DecimalFormatter.to1Decimal(v.dValue/Constants.MMOLL_TO_MGDL) + ",\"" + resourceHelper.gs(Units.Mmol_L.stringId()) + "\"" Units.Mg_Dl -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue) + ";" + resourceHelper.gs(Units.Mg_Dl.stringId()) else DecimalFormatter.to1Decimal(v.dValue/Constants.MMOLL_TO_MGDL) + ";" + resourceHelper.gs(Units.Mmol_L.stringId())
Units.Mmol_L -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue*Constants.MMOLL_TO_MGDL) + ",\"" + resourceHelper.gs(Units.Mg_Dl.stringId()) + "\"" else DecimalFormatter.to1Decimal(v.dValue) + ",\"" + resourceHelper.gs(Units.Mmol_L.stringId()) + "\"" Units.Mmol_L -> if (profileFunction.getUnits()==Constants.MGDL) DecimalFormatter.to0Decimal(v.dValue*Constants.MMOLL_TO_MGDL) + ";" + resourceHelper.gs(Units.Mg_Dl.stringId()) else DecimalFormatter.to1Decimal(v.dValue) + ";" + resourceHelper.gs(Units.Mmol_L.stringId())
Units.G -> v.iValue.toString() + ",\"" + resourceHelper.gs(Units.G.stringId()) + "\"" Units.G -> v.iValue.toString() + ";" + resourceHelper.gs(Units.G.stringId())
Units.U_H -> DecimalFormatter.to2Decimal(v.dValue) + ",\"" + resourceHelper.gs(Units.U_H.stringId()) + "\"" Units.U_H -> DecimalFormatter.to2Decimal(v.dValue) + ";" + resourceHelper.gs(Units.U_H.stringId())
else -> if (v.sValue != "") {"\""+ v.sValue.replace("\"", "\\\"") + if (!v.unit.stringId().equals(0)) "\",\"" + resourceHelper.gs(v.unit.stringId()) + "\"" else "\","} else -> if (v.sValue != "") {"\""+ v.sValue.replace("\"", "\"\"") + if (!v.unit.stringId().equals(0)) "\";\"" + resourceHelper.gs(v.unit.stringId()).replace("\"", "\"\"") + "\"" else "\";"}
else if (v.dValue != 0.0 || v.iValue != 0) { v.value().toString() + if (!v.unit.stringId().equals(0)) ",\"" + resourceHelper.gs(v.unit.stringId()) + "\"" else "," } else if (v.dValue != 0.0 || v.iValue != 0) { v.value().toString() + if (!v.unit.stringId().equals(0)) ";" + resourceHelper.gs(v.unit.stringId()) else ";" }
else "," else ";"
} }
} }
} }

View file

@ -229,8 +229,3 @@ fun Units.stringId(): Int {
else -> 0 else -> 0
} }
} }
fun UserEntry.toXml(): String {
val content = action.name + "," + values.joinToString(",") {value -> value.toXml()}
return ""
}

View file

@ -486,7 +486,7 @@
<string name="uel_tt_canceleted_from_ns">TT CANCELED FROM NS</string> <string name="uel_tt_canceleted_from_ns">TT CANCELED FROM NS</string>
<string name="uel_unknown">UNKNOWN</string> <string name="uel_unknown">UNKNOWN</string>
<string name="formated_string">Formated string</string> <string name="formated_string">Formated string</string>
<string name="ue_export_to_xml">Export User Entries to xml</string> <string name="ue_export_to_csv">Export User Entries to Excel (csv)</string>
<plurals name="days"> <plurals name="days">
<item quantity="one">%1$d day</item> <item quantity="one">%1$d day</item>

View file

@ -136,13 +136,6 @@ data class UserEntry(
if (!iValue.equals(0)) return iValue if (!iValue.equals(0)) return iValue
return lValue return lValue
} }
fun toXml() : String {
if (sValue != "") return "\""+ sValue.replace("\"", "\\\"") + "\"," + unit.text
if (!dValue.equals(0.0)) return dValue.toString() + "," + unit.text
if (!iValue.equals(0)) return return iValue.toString() + "," + unit.text
return return lValue.toString() + "," + unit.text
}
} }
enum class Units(val text: String) { enum class Units(val text: String) {
@SerializedName("None") None (""), @SerializedName("None") None (""),