在代碼中,我通過將其 id 粘貼到某個作業表來自動編輯 Google 檔案,更改一些變數(輸入在“輸入”選項卡中給出)并下載它的 pdf 版本。腳本首先創建它的 Google Doc 版本,然后創建它的 pdf 版本。但是,代碼只能操作 1 行。我如何確保它適用于所有行。非常感謝
function openModalDialog() {
const html = HtmlService.createHtmlOutputFromFile("Download").setTitle("Succeed");
SpreadsheetApp.getUi().showSidebar(html);
}
function createDataUrl(type) {
type = "pdf";
const blob = CreateReport();
const mimeTypes = { xlsx: MimeType.MICROSOFT_EXCEL, pdf: MimeType.PDF };
return {
data:
`data:${mimeTypes[type]};base64,`
Utilities.base64Encode(blob.getBytes()),
filename: `${blob.getName()}.${type}`,
};
}
function CreateReport(){
var id = SpreadsheetApp.getActiveSpreadsheet().getId();
var rawdata = Sheets.Spreadsheets.Values.get(id,'Input!A2:Z');
var linkdata = Sheets.Spreadsheets.Values.get(id,'Link!B1:B3');
const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const docid = ss.getRange("Link!B2").getValue();
const pdfid = ss.getRange("Link!B3").getValue();
const tempId = ss.getRange("Link!B1").getValue();
const folderdoc = DriveApp.getFolderById(docid);
const folderpdf = DriveApp.getFolderById(pdfid);
const templateId = DriveApp.getFileById(tempId);
for(var i = 0; i < rawdata.values.length; i ){
var check = rawdata.values[i][11];
if(check !== "Done"){
var Name = rawdata.values[i][0];
var var1 = rawdata.values[i][1];
var var2 = rawdata.values[i][2];
var var3 = rawdata.values[i][3];
var var4 = rawdata.values[i][4];
var var5 = rawdata.values[i][5];
var var6 = rawdata.values[i][6];
var var7 = rawdata.values[i][7];
var var8 = rawdata.values[i][8];
var var9 = rawdata.values[i][9];
var var10 = rawdata.values[i][10];
var newdoc = DriveApp.getFileById(tempId).makeCopy();
var documentId = newdoc.getId();
var doc = DocumentApp.openById(documentId);
DriveApp.getFileById(documentId).setName(Name);
var body = DocumentApp.openById(documentId).getBody();
body.replaceText('#1#', rawdata.values[i][1]);
body.replaceText('#2#', rawdata.values[i][2]);
body.replaceText('#3#', rawdata.values[i][3]);
body.replaceText('#4#', rawdata.values[i][4]);
body.replaceText('#5#', rawdata.values[i][5]);
body.replaceText('#6#', rawdata.values[i][6]);
body.replaceText('#7#', rawdata.values[i][7]);
body.replaceText('#8#', rawdata.values[i][8]);
body.replaceText('#9#', rawdata.values[i][9]);
body.replaceText('#10#', rawdata.values[i][10]);
folderdoc.addFile(DriveApp.getFileById(documentId));
doc.saveAndClose();
var docblob = doc.getBlob();
docblob.setContentType
docblob.setName(doc.getName() ".pdf");
folderpdf.createFile(docblob);
ss.getRange(i 2, 12).setValue("Done");
SpreadsheetApp.flush();
return docblob;
}
}
}
<h1>Downloading</h1>
<p>Please Wait...</p>
<script>
google.script.run
.withSuccessHandler(({ data, filename }) => {
const a = document.createElement("a");
document.body.appendChild(a);
a.download = filename;
a.href = data;
a.click();
google.script.host.close();
})
.createDataUrl("pdf");
</script>
uj5u.com熱心網友回復:
但是,代碼只能操作 1 行。
這是因為在For回圈中,您在第一次迭代時回傳Blob資料的第一行
for(var i = 0; i < rawdata.values.length; i ){
var check = rawdata.values[i][11];
if(check !== "Done"){
return docblob; // Right Here
}
}
現在要發送多個檔案的 blob,您需要一次發送所有 blob,因此您必須將它們添加到array.
我還稍微修改了您的代碼,在您的服務器端進行更改:-
function createDataUrl(type) {
type = "pdf";
const blobs = CreateReport();
const mimeTypes = { xlsx: MimeType.MICROSOFT_EXCEL, pdf: MimeType.PDF };
const blob_info = []
blobs.forEach(r=> blob_info.push([
{
data:
`data:${mimeTypes[type]};base64,`
Utilities.base64Encode(r.getBytes()),
filename: `${r.getName()}`
}
]))
return blob_info;
}
function CreateReport(){
var ssMain = SpreadsheetApp.getActiveSpreadsheet();
const id = ssMain.getId()
const rawdata = Sheets.Spreadsheets.Values.get(id,'Input!A2:Z');
const linkdata = Sheets.Spreadsheets.Values.get(id,'Link!B1:B3');
const ss = ssMain.getActiveSheet();
const docid = ss.getRange("Link!B2").getValue();
const pdfid = ss.getRange("Link!B3").getValue();
const tempId = ss.getRange("Link!B1").getValue();
const folderdoc = DriveApp.getFolderById(docid);
const folderpdf = DriveApp.getFolderById(pdfid);
const templateId = DriveApp.getFileById(tempId);
const fileBlobs = [] // We will use it to store info of all blob
for(var i = 0; i < rawdata.values.length; i ){
var check = rawdata.values[i][11];
if(check !== "Done")
{
var Name = rawdata.values[i][0];
var newdoc = DriveApp.getFileById(tempId).makeCopy();
var documentId = newdoc.getId();
var doc = DocumentApp.openById(documentId);
DriveApp.getFileById(documentId).setName(Name);
var body = DocumentApp.openById(documentId).getBody();
body.replaceText('#1#', rawdata.values[i][1]);
body.replaceText('#2#', rawdata.values[i][2]);
body.replaceText('#3#', rawdata.values[i][3]);
body.replaceText('#4#', rawdata.values[i][4]);
body.replaceText('#5#', rawdata.values[i][5]);
body.replaceText('#6#', rawdata.values[i][6]);
body.replaceText('#7#', rawdata.values[i][7]);
body.replaceText('#8#', rawdata.values[i][8]);
body.replaceText('#9#', rawdata.values[i][9]);
body.replaceText('#10#', rawdata.values[i][10]);
folderdoc.addFile(DriveApp.getFileById(documentId));
doc.saveAndClose();
var docblob = doc.getBlob();
docblob.setContentType
docblob.setName(doc.getName() ".pdf");
folderpdf.createFile(docblob);
ss.getRange(i 2, 12).setValue("Done");
SpreadsheetApp.flush();
fileBlobs.push(docblob)
}
}
return fileBlobs
}
并在您的客戶端進行更改:-
<script>
google.script.run.withSuccessHandler(function(r){
for (var i = 0 ; i < r.length; i )
{
var a = document.createElement("a");
document.body.appendChild(a);
a.download = r[i][0].filename;
a.href = r[i][0].data;
a.click();
}
google.script.host.close()
}).createDataUrl("pdf");
//
</script>
參考:
回傳
uj5u.com熱心網友回復:
在您的 if 塊中,一旦執行一次,它就不會回圈,而只會回傳。但是如果你想做多個 Doc 檔案,那么 createDataUrl() 只適用于 1 個 blob。所以你的邏輯逃脫了我。
for(var i = 0; i < rawdata.values.length; i ){
var check = rawdata.values[i][11];
if(check !== "Done"){
...
return docblob;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/432783.html
標籤:谷歌应用脚本
