Google Apps Script를 사용하여 Google 드라이브에서 두 가지 작업을 수행하려고합니다.
1 단계가 작동 중입니다. Google 문서 파일이 내부에서 업데이트 된 값으로 생성되었습니다.
2 단계 : 두 가지 질문/문제가 있습니다.
아이디어가 있습니까?
다음은 코드입니다.
function replaceTags () {
// Global vars
var ui = DocumentApp.getUi ();
var date = Utilities.formatDate(new Date(), "GMT", "dd-MM-yyyy");
var folderOutId = 'xxxxxxxxxxxxxxxxxxxx';
var tplMaintenanceId = 'xxxxxxxxxxxxxxxxxxxx';
var fileOutName = 'my file name';
var clientCompany = ui.prompt ('Company Name :');
// Do a copy from template file to a new file inside a specific folder
targetFolder = DriveApp.getFolderById (folderOutId);
var documentId = DriveApp.getFileById (tplMaintenanceId). makeCopy (targetFolder). getId ();
// Rename filed copied
DriveApp.getFileById (documentId) .setName (fileOutName + ' - ' + clientCompany.getResponseText ());
// Add document body inside variable
var body = DocumentApp.openById (documentId) .getBody ();
// Insert data inside Google Doc document
body.replaceText ('##DATE##', date);
body.replaceText ('##CLIENT_COMPANY##', clientCompany.getResponseText ());
gdocToPDF(documentId);
}
function gdocToPDF(fileID) {
var documentRootfolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxx") // replace this with the ID of the folder that contains the documents you want to convert
var pdfFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxx"); // replace this with the ID of the folder that the PDFs should be put in.
var docFile = DriveApp.getFileById(fileID)
createPDF(docFile.getId(), pdfFolder.getId(), function (fileID, folderID) {
if (fileID) createPDFfile(fileID, folderID);
}
)
}
function createPDF(fileID, folderID, callback) {
var templateFile = DriveApp.getFileById(fileID);
var templateName = templateFile.getName();
var existingPDFs = DriveApp.getFolderById(folderID).getFiles();
//in case no files exist
if (!existingPDFs.hasNext()) {
return callback(fileID, folderID);
}
for (; existingPDFs.hasNext();) {
var existingPDFfile = existingPDFs.next();
var existingPDFfileName = existingPDFfile.getName();
if (existingPDFfileName == templateName + ".pdf") {
Logger.log("PDF exists already. No PDF created")
return callback();
}
if (!existingPDFs.hasNext()) {
Logger.log("PDF is created")
return callback(fileID, folderID)
}
}
}
function createPDFfile(fileID, folderID) {
var templateFile = DriveApp.getFileById(fileID);
var folder = DriveApp.getFolderById(folderID);
var theBlob = templateFile.getBlob().getAs('application/pdf');
var newPDFFile = folder.createFile(theBlob);
var fileName = templateFile.getName().replace(".", ""); //otherwise filename will be shortened after full stop
newPDFFile.setName(fileName + ".pdf");
}
-
답변 # 1
관련 자료
- javascript - 너무 많은 동시 호출 Google Script
- google apps 스크립트 - createalldayevent (string, string, object) 메소드를 찾을 수 없습니다
- Google 스크립트에서 텍스트를 숫자로 변환하는 방법
- Google 앱 스크립트 속성 서비스 | 보안 및 재산 평생
- JDBC를 통한 Google Apps 스크립트가있는 SQL Server
- Google Apps 스크립트를 사용하여 모든 Google 설문지를 동일한 시트 gid로 보내기
- Google 클라우드에서 항상 Python 스크립트 실행
- JavaScript POST 요청을 Google Apps Script 코드로 변환
- Google 앱 스크립트 서비스 요청을 줄이는 방법
- Google Apps 스크립트에서 파일 바인딩 된 스크립트에 대한 스크립트 권한을 프로그래밍 방식으로 복사하는 방법은 무엇입니까?
- oauth - 액세스 토큰이있는 웹앱으로 배포 된 Google Apps Script에 액세스 할 수 없습니다
- 데이터 입력시 새 Google 스프레드 시트를 자동으로 작성하는 스크립트 작성
- javascript - Google 스크립트에서 비어 있지 않은 셀의 범위를 얻는 방법
- Google Apps Script. JSON to Spreadsheet - google apps script json에서 스프레드 시트로 - 결과가 너무 김 (api to sheet) 100 reg에서만 작동
- Google 스크립트로 하이퍼 링크 셀을 자동으로 여는 방법은 무엇입니까?
- javascript - Google App Script에서 한 시트의 원시 데이터를 새 시트의 별도 탭으로 정렬
- javascript - google 스크립트 - 웹 앱을 사용할 때만 doget 함수를 찾을 수 없습니까?
- replaceAllText를 사용하는 RegEx Google 앱 스크립트
- 클라우드 함수에서 Google Apps 스크립트로 매개 변수를 전달하고 응답을 반환 할 수 없습니다
코드를 다시 만들려고했고 문서를 pdf로 성공적으로 변환했습니다.
할 수있는 일은 PDF로 변환 할 문서 만 허용하도록 gdocToPDF () 함수를 편집하는 것입니다. 아래 샘플 코드를 참조하십시오 (원하는 방식이 아닐 수도 있음).
replaceTags () 함수에 사용 된 문서 ID를 gdocToPDF ()에 전달합니다.
드라이브 폴더의 모든 파일을 가져 오는 대신이 코드를 사용하여 대체 된 태그 만있는 파일을 사용하십시오.
이렇게하면 변환하려는 유일한 문서로 문서를 pdf로 변환 할 수 있습니다.
앞으로 좋은 하루 되세요!