简体   繁体   English

如何使用相对路径指向我希望将数据导入数据模型的文件?

[英]How do I use a relative path to point to a file from which I wish to import data to my Data Model?

What I'm really trying to achieve 我真正想要实现的目标

We have an Excel dashboard which is built to be used in tandem with an Export from our in-house application. 我们有一个Excel仪表板,可以与我们内部应用程序的Export一起使用。 The generation of the data Excel file is done with EPPlus and we were convinced that we could use the Excel Dashboard as the "base" file for the generation, so that the exported file contained the dashboard in one worksheet and the dataset in another worksheet. 使用EPPlus生成数据Excel文件,我们确信我们可以使用Excel仪表板作为生成的“基础”文件,以便导出的文件包含一个工作表中的仪表板和另一个工作表中的数据集。

So the user would receive everything they needed in one single file. 因此,用户将在一个文件中收到他们需要的所有内容。

We sort of got going with this thanks to named ranges which dynamically adapt to the size of the dataset, covered here 由于命名范围动态适应数据集的大小,我们对此有所了解

However, we've found, that since the DataModel in the Excel dashboard file is an OLAP Cube, we can't update the underlying dataset and save it with EPPlus and receive an error about "the cache source is not a worksheet" when trying to save the sheet. 但是,我们发现,由于Excel仪表板文件中的DataModel是OLAP多维数据集,我们无法更新底层数据集并使用EPPlus保存它并在尝试时收到有关“缓存源不是工作表”的错误保存表格。

So while we worked on that, we sought out a temporary solution... where we've also hit a snag. 因此,在我们努力实现这一目标的同时,我们找到了一个临时解决方案......我们也遇到了障碍。

The new problem 新问题

The temporary solution we thought of is to distribute the Excel dashboard and the data as two separate Excel files. 我们想到的临时解决方案是将Excel仪表板和数据分发为两个单独的Excel文件。 The Dashboard is distributed to those who need it and the data exports can then be generated from our application. 仪表板分发给需要它的人,然后可以从我们的应用程序生成数据导出。

We thought that the only downside of this would be that it would require the user to rename and place the data file side by side with the Excel Dashboard manually. 我们认为这样做的唯一缺点是它需要用户手动重命名并将数据文件与Excel Dashboard并排放置。

However, we are running into an issue where Excel is insisting on using the absolute path to the data file instead of a relative path. 但是,我们遇到的问题是Excel坚持使用数据文件的绝对路径而不是相对路径。

This results in requiring the user to point the source to the data export manually. 这导致要求用户手动将源指向数据导出。 Which is, apparently, done this way: 显然,这是这样做的: 更新外部Excel数据文件的路径的方法

Now, I'm showing the entire process to also give some insight into the way we've set it up, since I'm not sure I'm using the right words about the technical details - and perhaps I'm grossly mistaken in the approach. 现在,我正在展示整个过程,以便对我们设置它的方式有所了解,因为我不确定我是否正在使用关于技术细节的正确词语 - 也许我错误地认为该方法。

Overall, the way it works 总的来说,它的工作方式

File A contains the dashboard, some sheets with pivot tables used by the dashboard. File A包含仪表板,一些表格与仪表板使用的数据透视表。 The pivot tables are all working off of the aforementioned Data Model in the sheet which is a Cube (we need it to be a cube for some of the functions we use in the pivot tables). 数据透视表都是在上述数据模型中处理的,这是一个多维数据集(我们需要将它作为我们在数据透视表中使用的一些函数的多维数据集)。 The Data Model is based on a named range including all the data in one of the worksheets. 数据模型基于命名范围,包括其中一个工作表中的所有数据。

File B is the one generated by our application. File B是我们的应用程序生成的File B The two files were placed side by side when designing the dashboard and mapping the data since I was of the impression that Excel attempted to keep relative file paths when possible. 在设计仪表板和映射数据时,这两个文件并排放置,因为我的印象是Excel尽可能保留相对文件路径。 The file consists of a single worksheet containing the exported data. 该文件由包含导出数据的单个工作表组成。

At this point, the challenge was to automatically pull in data from File B into the worksheet in File A . 此时,挑战在于自动将File B中的数据导入到File A的工作表中。 I did this by going to the Data tab and using the Get Data function to point it to File B and telling Excel to load the data from the specified sheet. 我这样做是通过转到数据选项卡并使用Get Data功能将其指向File B并告诉Excel加载指定工作表中的数据。

This worked like a charm although the "solution" seems overly complicated. 虽然“解决方案”看起来过于复杂,但这很有吸引力。 All was well until we tried to use sheet from another machine/directory. 一切顺利,直到我们尝试使用另一台机器/目录中的工作表。 Then we found that the path to File B seemed to be absolute and that the data file could no longer be found. 然后我们发现File B的路径似乎是绝对的,并且无法再找到数据文件。

So, the short question after this long-winded explanation is: " How can I possibly have Excel use a relative path to another file, when importing data from an external file with the "Get Data" function? " 因此,在这个冗长的解释之后的简短问题是:“ 当使用”获取数据“功能从外部文件导入数据时,我怎么可能让Excel使用另一个文件的相对路径?

By getting more familiar with Power Query I've been able to come up with a solution to this. 通过更加熟悉Power Query,我已经能够为此提出解决方案。

My setup is still as described above. 我的设置仍然如上所述。

First, I have a sheet with some "system" values I use various places in the Workbook. 首先,我有一个带有一些“系统”值的工作表我在工作簿中使用了不同的位置。 I added a field there with the following Excel formula: 我在那里添加了一个字段,其中包含以下Excel公式:

=LEFT(CELL("filename");FIND("[";CELL("filename");1)-1)

This provides me with the absolute path to the folder where my File A resides. 这为我提供了File A所在文件夹的绝对路径。 I use this value, and concatenate the expected filename of File B which holds the data. 我使用此值,并连接保存数据的File B的预期文件名。 The result is an absolute path, pointing to where I expect the data file to be located. 结果是一个绝对路径,指向我期望数据文件所在的位置。

I then added a name range pointing to the exact cell where this value is contained. 然后我添加了一个名称范围,指向包含此值的确切单元格。

Next, I added a new Power Query function like this: 接下来,我添加了一个新的Power Query函数,如下所示:

= (rangeName) => Excel.CurrentWorkbook(){[Name=rangeName]}[Content]{0}[Column1]

The function takes the name of a named range as a parameter and spits back the value. 该函数将命名范围的名称作为参数并吐出该值。 I my case I called the function GetValue . 我的情况下我调用了函数GetValue This function can now be used in other Power Query scripts. 此功能现在可以在其他Power Query脚本中使用。

Finally, I loaded up the Power Query scripts which is responsible for loading the data from the other Excel sheet. 最后,我加载了Power Query脚本,负责从其他Excel工作表加载数据。 In that script, I changed the path of the file to this: 在该脚本中,我将文件的路径更改为:

Source = Excel.Workbook(File.Contents(GetValue("FilePath")), null, true),

The single thing to notice here is the call of GetValue("FilePath") for the path parameter of File.Contents . 这里需要注意的一件事就是为File.Contents的path参数调用GetValue("FilePath") FilePath is the name I gave the range pointing to the cell. FilePath是我给出指向单元格的范围的名称。 All it does, is load the path from my sheet and use that as the path for the Excel sheet holding the data. 它只是从我的工作表加载路径并将其用作保存数据的Excel工作表的路径。

A quite convoluted solution, but it works. 一个非常复杂的解决方案,但它的工作原理。

暂无
暂无

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

相关问题 如何在asp .net / c#项目代码中使用脚本文件夹的相对路径? - how do i use the relative path to the scripts folder in my asp .net / c# project code? 在针对已编码的ui的调试中运行代码时,如何获取文件的相对路径 - How do i get a relative path of a file when running my code in debug for coded ui 如何在原型文件导入语句中使用相对路径? - How can I use relative paths in proto file import statement? 如何在XDocument.Load中使用相对路径? - How do I use a relative path in XDocument.Load? 如何使用相对路径引用WSDL文件? - How do I reference a WSDL file with a relative path? 如何在我的视图模型中从域服务访问数据? Silverlight 5应用 - How do I access the data from my domain service in my view model ? Silverlight 5 application 如何将模型中的数据添加到具有相同字段的模型? - How do I add data from a model to a model with the same field? 如何从文本文件中读取数据,然后使用新计算编写新文本文件并使用我的子类方法进行计算? - How do I read data from a text file, then write a new text file with new calculations and use my subclass methods for calculations? 我如何在设置中使用字符串文件路径,以便我可以在我的代码中使用该路径 - How do i use a string file path in settings so i can get that path useable in my code 如何将WPF与XML绑定用于具有从兄弟姐妹到父级的相关数据的指定路径 - How do I use WPF binding with XML for a specified path with related data from siblings to the parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM