简体   繁体   English

使用上面最接近的填充单元格的值填充Excel工作表(或SQLite)中的空单元格

[英]Fill empty cells in excel sheet (or SQLite) with the value of nearest filled cell above

I have faced an issue when sending queries to a SQLite database. 我在向SQLite数据库发送查询时遇到了问题。

The problem is that the database (which origins from an excel sheet) has empty cells where there should be values, values that are given from a cell above, in the same column. 问题是数据库(源自excel表)具有空单元格,其中应该存在值,即从上面的单元格给出的值,在同一列中。

In other words, the maker of the excel sheet has left the cells in he same column blank if the value does not change. 换句话说,如果值没有改变,excel表的制造者将单元格中的单元格留空。 I actually need values in those cells! 我实际上需要那些细胞中的值!

I can not do this manually due to the size of the sheet/db, is there a way to do this programmaticly? 由于工作表/数据库的大小,我不能手动执行此操作,有没有办法以编程方式执行此操作?

I would really prefer a sql-query way, if there is any! 我真的更喜欢sql-query方式,如果有的话!

Best wishes, Tor 祝愿,Tor

you can do this in excel. 你可以在excel中做到这一点。 I answered a similar question here: 我在这里回答了类似的问题:

Blank Values in Excel File From CSV (not just rows) CSV文件中的空白值(不仅仅是行)

You can also do this entirely in excel: 您也可以在excel中完全执行此操作:

Select column (or whatever range you're working with), then go to Edit>Go To (Ctrl+G) and click Special. 选择列(或您正在使用的任何范围),然后转到编辑>转到(Ctrl + G)并单击特殊。 Check Blanks & click OK. 检查空白并单击“确定”。 This will select only the empty cells within the list. 这将仅选择列表中的空单元格。 Now type the = key, then up arrow and ctrl-enter. 现在输入=键,然后按向上箭头并按住Ctrl键。

This will put a formula in every blank cell to equal the cell above it. 这将在每个空白单元格中放置一个公式,使其等于其上方的单元格。 You could then copy ^ paste values only to get rid of the formulas. 然后,您可以复制^粘贴值以删除公式。

A similar way to the one proposed by @maneesha would be to filter by blank cells, then write the "equals to the upper cell" formula and then clear the filter. 与@maneesha提出的方法类似的方法是用空白单元格过滤,然后写入“等于上单元格”公式,然后清除过滤器。

But I think @maneeshas' solution is cooler. 但我认为@maneeshas的解决方案更酷。

You query the whole db, keep the original sort order and then iterate over each row. 您查询整个数据库,保留原始排序顺序,然后遍历每一行。 If something is empty update it with the value from above. 如果某些内容为空,请使用上面的值更新它。

Edit: you might do it with something similar to this here: 编辑:你可能会在这里做类似的事情:

SQLiteDatabase db = SQLiteDatabase.openDatabase("myDb.db", null, SQLiteDatabase.OPEN_READWRITE);
Cursor c = db.query("table", null, null, null, null, null, "the column that defines the sort order");
if (c != null) {
    int columnCount = c.getColumnCount();
    String[] lastValues = new String[columnCount];
    while (c.moveToNext()) {
        ContentValues cv = new ContentValues();
        for (int i = 0; i < columnCount; i++) {
            String value = c.getString(i);
            if (TextUtils.isEmpty(value)) {
                // if that happens on first column you are screwed anyways.
                cv.put(c.getColumnName(i), lastValues[i]);
            } else {
                lastValues[i] = value;
            }
            // we need to save some id here
        }
        if (cv.size() > 0) {
            // use the id here
            db.update("table", cv, "column=?", new String[] { "id" });
        }
    }
    c.close();
}

But i'd suggest you use Excel to fix the database. 但我建议您使用Excel来修复数据库。 That's easier and you don't need to understand above code. 这更容易,您不需要理解上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM