当前工作表为PDF到所需的Google云端硬盘位置 [英] Current sheet as PDF to Desired Google Drive Location

查看:40
本文介绍了当前工作表为PDF到所需的Google云端硬盘位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力编写代码,以将特定的工作表另存为pdf,并将其保存在所需的Drive位置,但无法正常工作.我在Google工作表脚本方面不擅长,但尝试学习.

表格=发票

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文件夹= DriveApp.getFolderById(fid);folder.createFile(pdf);} 

我试图粘贴ID和URL,但粘贴ID出了点问题,请您在代码中回答问题.

  function myFunction(){//工作表IDconst ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/17ZJmm4FKuDgtJRxuUMlgX3UA_yqSshhzKdqX2vr2kRw/edit#gid=730374715");const sheet = ss.getSheetByName("PaySlip");//将在其中粘贴哪个ID?const ssID = ss.getId(17ZJmm4FKuDgtJRxuUMlgX3UA_yqSshhzKdqX2vr2kRw);//哪个ID也要粘贴在这里?const shID = sheet.getSheetId(1RTG7fwafO6Nro6mj4KZkZHjhyYpdTbAt).toString();const nameFile = sheet.getRange("A2").getValue().toString()+.pdf";const fid ='abcdefghij1234567890';const文件夹= DriveApp.getFolderById(fid);const requestData = {方法":"GET",标题":{"Authorization":"Bearer" + ScriptApp.getOAuthToken()}};//将在其中粘贴哪个网址?const url =" https://docs.google.com/spreadsheets/d/" + ssID + quot;/export?format = pdf& id =" + ssID +"& gid =" + shID;const result = UrlFetchApp.fetch(url,requestData);folder.createFile(result.getBlob()).setName(nameFile);} 

解决方案

说明:

  • 您当前的解决方案不是创建工作表pdf的正确方法.您需要使用 getOAuthToken() Class UrlFetchApp .

  • 您的目标是基于发票表生成pdf,因此您需要找到 Invoice 表的表ID,并将其包含在 url 元素.

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

  const requestData = {方法":"GET",标题":{"Authorization":"Bearer" + ScriptApp.getOAuthToken()}};const url =" https://docs.google.com/spreadsheets/d/" + ssID + quot;/export?format = pdf& id =" + ssID +"& gid =" + shID;const result = UrlFetchApp.fetch(url,requestData); 

解决方案:

  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文件夹= DriveApp.getFolderById(fid);const requestData = {方法":"GET",标题":{"Authorization":"Bearer" + ScriptApp.getOAuthToken()}};const url =" https://docs.google.com/spreadsheets/d/" + ssID + quot;/export?format = pdf& id =" + ssID +"& gid =" + shID;const result = UrlFetchApp.fetch(url,requestData);folder.createFile(result.getBlob()).setName(nameFile);} 

I have been striving to make a code to save a specific sheet as pdf on a desired Drive location but its not working. I am no good in google sheet scripts but trying to learn.

Sheet = Invoice

B5 = Name of PDF file

Any help would be highly appreciated.

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

I have tried to paste the ID and URL something is wrong with pasting ID's can you please answer the question in Code.

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

解决方案

Explanation:

  • Your current solution is not the proper way to create a pdf of a sheet. You need to use getOAuthToken() and Class UrlFetchApp.

  • Your goal is to generate a pdf based on the Invoice sheet, therefore you need to find the sheet id of the Invoice sheet and include it to the url element.

The main addition you need to make in your code is:

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

Solution:

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

这篇关于当前工作表为PDF到所需的Google云端硬盘位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆