gpt4 book ai didi

c# - 该命令需要至少两行源数据

转载 作者:行者123 更新时间:2023-12-02 09:11:50 24 4
gpt4 key购买 nike

我收到此错误:

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

间歇性地在这行代码上:

xlWorkBook.RefreshAll();

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

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

这是怎么回事?为什么我有时会收到此错误?

非常感谢您的指导。

如果有帮助,我将提供整个方法:

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);
}
}

最佳答案

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

GetPivotData Excel 函数中,数据透视字段通过其名称进行引用 (=GETPIVOTDATA("EmailAddress",$A$3));因此,禁止不包含它们的数据源是有意义的。

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

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

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

    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);
}
}

也有可能在循环的 if (cell.Value2.ToString() == email ) 条件中永远找不到 email,这最终会导致删除范围中的所有行 - 即使唯一的区别是单元格内值末尾有一个额外的空格。使用上述代码,即使所有电子邮件地址都被删除,数据源对于连接到它的数据透视表来说仍然是有效的。

编辑:在 Excel 中,您可以通过选择相关范围并单击格式化为表格功能区按钮,将 Range 转换为 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";

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

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

然后,您可以通过几种不同的方式访问特定的ListObject/table:

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

随着表中行的增加,其引用的实际范围也会相应扩大;使用 myTable.Range 访问相关范围。

关于c# - 该命令需要至少两行源数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12755248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com