热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

此命令至少需要两行源数据-Thiscommandrequiresatleasttworowsofsourcedata

Iamgettingthiserror:我收到此错误:Thiscommandrequiresatleasttworowsofsourcedata.Youcannot

I am getting this error:

我收到此错误:

This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following:

- If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again.
- I you're creating a PivotTable, type a cell reference or select a range that includes at least two rows of data

intermittently on this line of code:

间歇性地在这行代码上:

xlWorkBook.RefreshAll();

There are two worksheets. One has a pivot table and one has raw data. Sometimes there is only one row of data. For multiple rows of data the line of code above always works; however, for only 1 row of data, the code above sometimes works, and sometimes I get the error message above.

有两个工作表。一个有一个数据透视表,一个有原始数据。有时只有一行数据。对于多行数据,上面的代码行始终有效;但是,对于只有1行数据,上面的代码有时会起作用,有时我会收到上面的错误消息。

In addition to this, the worksheet containing the pivot table is not refreshed; however, if I re-open the file, it also does not refresh, unless I explicitly refresh it manually.

除此之外,包含数据透视表的工作表不会刷新;但是,如果我重新打开文件,它也不会刷新,除非我手动显式刷新它。

What is going on here? Why am I getting this error only sometimes?

这里发生了什么?为什么我有时会收到此错误?

Thank you so much for your guidance.

非常感谢你的指导。

if at all helpful, i am including the entire method:

如果有帮助,我包括整个方法:

private void SortandCreateFile(string column, string email, string emailStartPos) {
    string replacetext = "";

    try {
        var valueRange = xlWorkSheet.get_Range(column + emailStartPos, column + range.Rows.Count.ToString());
        var deleteRange = valueRange;
        xlApp.Visible = false;
        int startpos = 0;
        int endPos=0;
        bool foundStart = false;

        Excel.Range rng = xlWorkSheet.get_Range(column + "1", column + range.Rows.Count.ToString());

        string tempstring = "d";
        int INTemailStartPos = Convert.ToInt16(emailStartPos);

        for (int rCnt = INTemailStartPos; rCnt <= rng.Count; rCnt++) {
            Excel.Range cell = (Excel.Range)rng[rCnt, 1];

            try {
                if (cell.Value2 != null)
                    tempstring = cell.Value2.ToString();
                else {
                    startpos = rCnt;
                    releaseObject(cell);  /////////
                    break;
                }
            }
                catch (Exception ee)
            {
            MessageBox.Show(ee.ToString());
        }
        //grab the text from column link texdtbox
        Excel.Range rngLinkColumn;
        Excel.Range replacetextcell=null;

        if (FormControls.ColumnLink.Length > 0) {
            rngLinkColumn = xlWorkSheet.get_Range(FormControls.ColumnLink + "1", FormControls.ColumnLink + range.Rows.Count.ToString());
            replacetextcell = (Excel.Range)rngLinkColumn[rCnt, 1];
        }    
        //locate email
        if (cell.Value2.ToString() == email ) {
            //we found the starting position of the email we want!
            //this will tell us which row of data to start from
            startpos = rCnt;

            if (FormControls.ColumnLink.Length > 0)
                replacetext = replacetextcell.Value2.ToString();
            releaseObject(cell);  /////////
            break;
        }
        releaseObject(cell);
    }
    int foundstartminusOnE= startpos - 1;
    int rngcount = rng.Count + INTemailStartPos;

    //delete everything from the top UNTIL the row of the email address that we need
    if (startpos != INTemailStartPos) {
        deleteRange = xlWorkSheet.get_Range(column + INTemailStartPos.ToString() + ":" + "CF" + foundstartminusONE.ToString(), Type.Missing);
        deleteRange = deleteRange.EntireRow;
        deleteRange.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
    }

    for (int rCnt = INTemailStartPos; rCnt <= rng.Count; rCnt++) {
        Excel.Range cell = (Excel.Range)rng[rCnt, 1];

        try {
            if (cell.Value2 != null )
                tempstring = cell.Value2.ToString();
            else {
                endPos = rCnt - 1;
                releaseObject(cell);////////
                break;
            }
        }
        catch (Exception ee) {
            //MessageBox.Show(ee.ToString());
        }    
        //locate email
        if (cell.Value2.ToString() != email ) {
            //we found where the last email address is that we need
            //this is where the issue is occurring i think with the deleting the last row
            endPos = rCnt;
            releaseObject(cell);////////
            break;
        }
        releaseObject(cell);
    }

    //delete all the stuff AFTER the email address that we need
    if (endPos != 0) {
        deleteRange = xlWorkSheet.get_Range(column + endPos + ":" + "CF" + rngcount.ToString(), Type.Missing);
        deleteRange = deleteRange.EntireRow;
        deleteRange.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
    }

    //when the user opens the excel file, we want the focus to be here
    var rangehome = xlWorkSheet.get_Range(FormControls.FocusOn, FormControls.FocusOn);
    xlWorkSheet.Activate();
    rangehome.Select();

    string filename = xlWorkBook.Path + @"\" + email + ".xlsx";
    string fileSubstring = filename.Substring(0, filename.IndexOf(".xlsx"));
    string randomfileString = Guid.NewGuid().ToString("N").Substring(0, 10) + ".xlsx";
    string targetfilenameRename = fileSubstring + randomfileString;

    //((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[FormControls.WorksheetFocus]).Activate();
    //((Excel.Worksheet)Excel.Application.ActiveWorkbook.Sheets[1]).Activate();  

    Excel.Worksheet xlWorkSheetFocus = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(FormControls.WorksheetFocus);
    xlWorkSheetFocus.Activate();
    xlWorkBook.SaveAs(targetfilenameRename, Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
                false, false, Excel.XlSaveAsAccessMode.xlNoChange,
                Type.Missing, Type.Missing, Excel.XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);

    try {
        xlWorkBook.RefreshAll();
    }
    catch { }
        xlWorkBook.Save();
        string targetfile = xlWorkBook.Path + @"\" + FormControls.FileName + " - "
                    + email.Substring(0, email.IndexOf("@")) + ".xlsx";
        System.IO.File.Copy(targetfilenameRename, targetfile, true);

        string body = FormControls.eMailBody;
        body = body.Replace("%replacetext%", replacetext);
        //replace %replacetext% in body
        string targetfileSubstring = targetfile.Substring(0, targetfile.IndexOf(".xlsx"));
        string randomString = Guid.NewGuid().ToString("N").Substring(0, 10)+".xlsx";
        string targetfileRename = targetfileSubstring+randomString;

        while (true) {
            try {
                SendEmail(targetfile, email, FormControls.eMailSubject, body,FormControls.eMailFrom);                                  
            }
            catch (Exception ee) {
                MessageBox.Show(ee.ToString());
                continue;
            }

            // all is good
            break;
        }
        releaseObject(valueRange);
        releaseObject(deleteRange);
        File.Copy(targetfile, targetfileRename, true);
    }
    catch (Exception e) {
        MessageBox.Show(e.ToString());
    }
    finally {
        //DisposeMe();
        // Release all COM RCWs.
        // The "releaseObject" will just "do nothing" if null is passed,
        // so no need to check to find out which need to be released.
        // The "finally" is run in all cases, even if there was an exception
        // in the "try". 
        // Note: passing "by ref" so afterwords "xlWorkSheet" will
        // evaluate to null. See "releaseObject".
        releaseObject(range);
        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        // The Quit is done in the finally because we always
        // want to quit. It is no different than releasing RCWs.
        if (xlApp != null) {
            xlApp.Quit();
        }
        releaseObject(xlApp);
    }
}

3 个解决方案

#1


6  

The only way I could replicate this error with a pivot table was by attempting to create one off a range that didn't have column headers, just like on the screenshot from Stephan1010's answer.

我可以使用数据透视表复制此错误的唯一方法是尝试在没有列标题的范围内创建一个,就像Stephan1010的答案截图一样。

In the GetPivotData Excel function, pivot fields are referred to by their names (=GETPIVOTDATA("EmailAddress",$A$3)); thus, it makes sense to disallow a data source that wouldn't have them.

在GetPivotData Excel函数中,透视字段由它们的名称引用(= GETPIVOTDATA(“EmailAddress”,$ A $ 3));因此,禁止不具备它们的数据源是有意义的。

The solution would be to pivot over a ListObject instead of a Range - in Excel when you select, say, range $A$1:$C$1 and format as table (from the Ribbon), the table that results will span $A$1:$C$2; the contents of the first row becomes the column headers and the second row is a valid, empty record. Interesting to note that this happens (the 2-row span) regardless of whether or not you check the "My table has headers" checkbox (the data will be moved to the first row and the table will contain default "Column1"-"Column2"-"Column3" headers if the checkbox is cleared).

当您选择范围$ A $ 1:$ C $ 1并格式化为表格(来自功能区)时,解决方案是在ListObject而不是Range上进行数据透视 - 结果表格将超过$ A $ 1: $ C $ 2;第一行的内容成为列标题,第二行是有效的空记录。有趣的是,无论您是否选中“我的表有标题”复选框,都会发生这种情况(2行跨度)(数据将移动到第一行,表格将包含默认的“Column1” - “Column2” “ - ”如果清除复选框,则为Column3“标题。

In other words, a ListObject is always a valid data source for a pivot table, while a Range may not contain enough rows. Also if you don't have column headers and you create a pivot table with range $A$1:$C$2, the record at $A$1:$C$1 will be used as column headers, which means that first record is lost.

换句话说,ListObject始终是数据透视表的有效数据源,而Range可能不包含足够的行。此外,如果您没有列标题并且创建范围为$ A $ 1:$ C $ 2的数据透视表,则$ A $ 1:$ C $ 1的记录将用作列标题,这意味着第一条记录将丢失。

From the code you have supplied I would presume the pivot table is already present and connected to some [named?] range in a template workbook that contains the macro. Turning your range into a table might be as trivial as selecting format as table from the Ribbon. And then you could have code like this to remove all unnecessary rows while still keeping a valid data source for the pivot table:

从你提供的代码我可以假设数据透视表已经存在并连接到包含宏的模板工作簿中的某个[named?]范围。将范围转换为表可能与从Ribbon中选择格式作为表格一样简单。然后,您可以使用这样的代码删除所有不必要的行,同时仍保留数据透视表的有效数据源:

    public void DeleteExtraTableRows(string emailAddress, Excel.ListObject table)
    {
        try
        {
            var rowIndex = 0;
            var wasDeleted = false;
            while (rowIndex <= table.ListRows.Count)
            {
                if (!wasDeleted) rowIndex++;
                var row = table.ListRows[rowIndex];

                var range = (Excel.Range)row.Range.Cells[1, 1];
                var value = range.Value2;

                if (value != null && !string.Equals(emailAddress, value.ToString()))
                {
                    row.Delete();
                    wasDeleted = true;
                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message + "\n\n" + e.StackTrace);
        }
    }

There is also a possibility that the email is never found in the loop's if (cell.Value2.ToString() == email ) condition, which would end up deleting all rows from your range - even if the only difference is an extra space at the end of the in-cell value. With the above code, even if all email addresses get deleted the data source remains a valid one for a pivot table that would be connected to it.

还有可能在循环的if(cell.Value2.ToString()== email)条件中找不到电子邮件,这最终会删除范围内的所有行 - 即使唯一的差异是在内嵌值的结束。使用上面的代码,即使删除了所有电子邮件地址,数据源仍然是与其连接的数据透视表的有效数据源。

EDIT: In Excel you turn a Range into a ListObject by selecting the range in question and clicking the Format as table Ribbon button, from the Home tab. Alternatively you can create one like this:

编辑:在Excel中,通过选择有问题的范围并单击“主页”选项卡中的“格式为表格功能”按钮,将范围转换为ListObject。或者你可以创建一个这样的:

            var range = ((Excel.Range)(worksheet.Range[worksheet.Cells[1, 1], worksheet.Cells[3, 1]]));
            var table = worksheet.ListObjects.Add(SourceType: Excel.XlListObjectSourceType.xlSrcRange, Source: range,
                                      XlListObjectHasHeaders: Excel.XlYesNoGuess.xlYes);
            table.TableStyle = "TableStyleMedium3";

In code, you can access all ListObjects on a worksheet using the ListObjects property:

在代码中,您可以使用ListObjects属性访问工作表上的所有ListObject:

        var worksheet = (Excel.Worksheet) Globals.ThisAddIn.Application.ActiveSheet;
        var tables = worksheet.ListObjects;

Then, you can access a specific ListObject /table with several different ways:

然后,您可以使用几种不同的方式访问特定的ListObject /表:

        var myTable = tables[1];
        var myTable = tables.Item["Table1"];
        var myTable = tables.OfType().FirstOrDefault(t => t.Name == "Table1");

As rows are added from the table, the actual range it refers to will be expanded accordingly; use myTable.Range to access the range in question.

当从表中添加行时,它引用的实际范围将相应地扩展;使用myTable.Range访问相关范围。

#2


4  

i suppose this situation occurs because of the pivot tables you got. cause refresh all will trigger pivot table's refresh command too. look at the code below. It may give you an idea about it. Its not about 1 row im sure. i checked it everthing works just fine its most posibly caused by pivot tables.

我想这种情况是因为您获得的数据透视表。导致刷新全部也将触发数据透视表的刷新命令。看下面的代码。它可能会让你了解它。它肯定不是一排。我检查了它的翻转工作就好了它最可能由枢轴表引起的。

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("some.xlsx");
// For each worksheet we got
foreach (Microsoft.Office.Interop.Excel.Worksheet worksheet in xlWorkbook.Sheets) 
{   // and each pivot table in each worksheet
    foreach (Microsoft.Office.Interop.Excel.PivotTable pivot in worksheet.PivotTables())
    {   // disable BackgroundQuery
        pivot.PivotTableWizard(BackgroundQuery: false);
    }
}
// try to refresh all sheet
try { xlWorkbook.RefreshAll(); } catch { }
// then save
xlWorkbook.Save();

#3


2  

The obvious answer seems to be that sometimes you have one row of data as the source for your pivot table and sometimes you don't - even when you think you still do. I have not been able to create a pivot table(or change the source of a pivot table) to one row of data:

显而易见的答案似乎是,有时你有一行数据作为数据透视表的来源,有时候你没有 - 即使你认为你仍然这样做。我无法创建数据透视表(或将数据透视表的源更改为一行数据):

PivotTableErrorExplanation

but if you are able to somehow figure out a way to do this then you have found your answer. There is no reason you can't have one row of data as your source just from a practical/theoretical perspective, but it looks like excel tries to prevent that from happening(maybe because the code assumes two rows). So if you do find a way, then it is probably a bug. Good Luck.

但如果你能够以某种方式找到一种方法来做到这一点,那么你已经找到了答案。从实际/理论的角度来看,没有理由不能将一行数据作为源,但看起来excel会尝试防止这种情况发生(可能因为代码假设有两行)。所以如果你找到了办法,那么它可能就是一个错误。祝你好运。


推荐阅读
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • Explain如何助力SQL语句的优化及其分析方法
    本文介绍了Explain如何助力SQL语句的优化以及分析方法。Explain是一个数据库SQL语句的模拟器,通过对SQL语句的模拟返回一个性能分析表,从而帮助工程师了解程序运行缓慢的原因。文章还介绍了Explain运行方法以及如何分析Explain表格中各个字段的含义。MySQL 5.5开始支持Explain功能,但仅限于select语句,而MySQL 5.7逐渐支持对update、delete和insert语句的模拟和分析。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • 大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记
    本文介绍了大数据Hadoop生态(20)MapReduce框架原理OutputFormat的开发笔记,包括outputFormat接口实现类、自定义outputFormat步骤和案例。案例中将包含nty的日志输出到nty.log文件,其他日志输出到other.log文件。同时提供了一些相关网址供参考。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
author-avatar
美玲珍任欢治
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有