Google apps script 以PDF格式显示的当前工作表到所需的Google驱动器位置

Google apps script 以PDF格式显示的当前工作表到所需的Google驱动器位置,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我一直在努力制作一个代码,将特定的工作表以pdf格式保存在所需的驱动器位置上,但它不起作用。我不擅长谷歌工作表脚本,但正在努力学习 Sheet=发票 B5=PDF文件名 任何帮助都将不胜感激 function myFunction() { const ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/17ZJmm4GHJUgtJRxuUMlgX3UA_yqSshhzKdqX2vr2kRw/edit

我一直在努力制作一个代码,将特定的工作表以pdf格式保存在所需的驱动器位置上,但它不起作用。我不擅长谷歌工作表脚本,但正在努力学习

Sheet=发票

B5=PDF文件名

任何帮助都将不胜感激

function myFunction() {
const ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/17ZJmm4GHJUgtJRxuUMlgX3UA_yqSshhzKdqX2vr2kRw/edit#gid=730295684");
const nameFile = ss.getSheetByName("Invoice").getRange("B5").getValue().toString() +".pdf"
var pdf = docurl.getBlob().setName(name).getAs('application/pdf');
    var fid = 'abcdefghij1234567890';
  var folder = DriveApp.getFolderById(fid);
  folder.createFile(pdf);
}
我尝试粘贴ID和URL粘贴ID有问题,请用代码回答问题

function myFunction() {
  //Sheet ID
const ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/17ZJmm4FKuDgtJRxuUMlgX3UA_yqSshhzKdqX2vr2kRw/edit#gid=730374715");
const sheet = ss.getSheetByName("PaySlip");
  // Which ID would be paste here ?
const ssID = ss.getId(17ZJmm4FKuDgtJRxuUMlgX3UA_yqSshhzKdqX2vr2kRw);
  // Which ID would be paste here as well ?
const shID = sheet.getSheetId(1RTG7fwafO6Nro6mj4KZkZHjhyYpdTbAt).toString();
const nameFile = sheet.getRange("A2").getValue().toString() +".pdf";
const fid = 'abcdefghij1234567890';  
const folder = DriveApp.getFolderById(fid);  
const requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};  
  // Which url would be paste here ?
const url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=pdf&id="+ssID+"&gid="+shID;
const result = UrlFetchApp.fetch(url , requestData);  
folder.createFile(result.getBlob()).setName(nameFile);   
}
说明:
  • 您当前的解决方案不是创建工作表pdf的正确方法。你需要使用和

  • 您的目标是基于
    Invoice
    工作表生成pdf,因此需要找到
    Invoice
    工作表的工作表id,并将其包含到
    url
    元素中

您需要在代码中添加的主要内容是:

const requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};  
const url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=pdf&id="+ssID+"&gid="+shID;
const result = UrlFetchApp.fetch(url , requestData);  
解决方案:
@很高兴这对你有帮助。快乐学习:)马里奥斯,你能指导如何获取“发票”选定区域的pdf文件吗?
function myFunction() {
const ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/17ZJmm4GHJUgtJRxuUMlgX3UA_yqSshhzKdqX2vr2kRw/edit#gid=730295684");
const sheet = ss.getSheetByName("Invoice");
const ssID = ss.getId();
const shID = sheet.getSheetId().toString();
const nameFile = sheet.getRange("B5").getValue().toString() +".pdf";
const fid = 'abcdefghij1234567890';  
const folder = DriveApp.getFolderById(fid);  
const requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};  
const url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=pdf&id="+ssID+"&gid="+shID;
const result = UrlFetchApp.fetch(url , requestData);  
folder.createFile(result.getBlob()).setName(nameFile);   
}