【问题标题】:Google sheets: How to save a single sheet in a spreadsheet file as PDF, where the file name is based on a certain cell content?Google表格:如何将电子表格文件中的单个表格保存为PDF,其中文件名基于某个单元格内容?
【发布时间】:2021-07-15 11:16:41
【问题描述】:

我觉得这是一个简单的问题,之前已经回答了很多,但是尽管我浪费了几个小时搜索这个网站(和其他网站)来简单、明确地回答我的问题,但我还是找不到一;因此我的问题。

上下文

我经常需要开具发票,提供每个生产/制造订单的详细信息。我最近将我们所有的数据库内容从 MS Excel 转移到了 Google 表格,以获得所有明显的好处(尤其是那些基于云的好处)。上述发票是使用电子表格创建的。该电子表格由多张表格组成,每张表格负责从不同的源电子表格中收集相关数据。包含主发票界面的主工作表导出为具有特定名称的 PDF 文件,该名称源自特定单元格(通常为 B6)。此单元格包含一个公式,显示其他几个单元格的组合,换句话说,此单元格不断变化并与其他单元格链接。

当我使用 Excel 时,我在互联网上搜索并能够创建 VBA 代码并将其链接到宏命令,这使我可以毫无问题地导出发票表,其名称源自上述单元格。当我激活宏时,文件会立即导出为 PDF 文件,其中的名称正是我想要的(即名称基于单元格 B6 的当前内容)。

问题

我无法让我在互联网上找到的任何资源 (like this one) 工作或复制他们拥有的资源,而且我无法自己从头开始创建一个。

我需要什么

如何在 Google 表格中实现这一点?如果这太容易或发布在其他地方,我真的很抱歉,但正如我之前所说,我的经验非常有限,我无法在其他任何地方找到我需要的东西。

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您正在寻找类似的东西吗?

    function exportSheet() {    
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var mainSheetName = 'Main Sheet'; //Change 'Main Sheet' to the name of your main sheet
      var mainSheet = ss.getSheetByName(mainSheetName);
      var name = mainSheet.getRange('B6').getValue(); //Gets the pdf name from cell B6
      var sheets = ss.getSheets(); //Pulls all sheets as an array
    
      for (var i = 0; i < sheets.length; i++) { //Iterates through all sheets
        if (sheets[i].getSheetName() !== mainSheetName) { //Hides all sheets that are not the main one
          sheets[i].hideSheet()
        }
      }
      
      DriveApp.createFile(ss.getBlob().getAs('application/pdf').setName(name)); //Exports the main sheet
    
      for (var i = 0; i < sheets.length; i++) { //Brings back all sheets that were hidden
        sheets[i].showSheet()
      }
    }
    

    我在模拟表中对此进行了测试,它对我有用。如果您对此有任何问题,或者我误解了某些内容,请告诉我。


    参考:Export Single Sheet to PDF in Apps Script

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多