我有一份备忘录,将谷歌透镜印成“人民抵抗力量”,并在我的驱动下予以挽救。 文字在这方面发挥了巨大作用。 面临的挑战是,我想要取代关于驾驶车的现有档案,保持战术上的联系,并保留许可,以便任何有链接的人都能查阅更新的档案。 下面是我的法典。 我如何调整,以便更新现有档案,而不是删除旧档案和提出新的档案?
function savePDF() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName( sheet_name ); // Specifically target the sheet_name sheet
var folderId = my_google_drive_folder_id ;
var pdfName = filename.pdf ;
// Determine the last non-empty cell in each column and row
var dataRange = sheet.getDataRange();
var lastRow = dataRange.getLastRow();
var lastColumn = dataRange.getLastColumn();
// Adjust print settings to fit the data range
var range = sheet.getRange(1, 1, lastRow, lastColumn);
sheet.setActiveRange(range);
spreadsheet.setActiveSheet(sheet, true);
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFilesByName(pdfName);
// Delete the existing PDF in the folder
while (files.hasNext()) {
var file = files.next();
folder.removeFile(file);
}
// Define PDF export URL with adjusted print range
var url = https://docs.google.com/spreadsheets/d/ + spreadsheet.getId() + /export?exportFormat=pdf&format=pdf ;
var options = &size=letter +
&portrait=true +
&scale=2 +
&top_margin=0.25 + &bottom_margin=0.75 + &left_margin=0.25 + &right_margin=0.25 +
&sheetnames=false&printtitle=false&pagenumbers=true&gridlines=false&fzr=true +
&gid= + sheet.getSheetId(); // Using the specific Sheet ID for exporting
var token = ScriptApp.getOAuthToken();
// Fetch the PDF
var response = UrlFetchApp.fetch(url + options, {
headers: {
Authorization : Bearer + token
}
});
// Convert to a blob and create new file in the drive folder
var blob = response.getBlob().setName(pdfName);
folder.createFile(blob);
// Log a success message
Logger.log( PDF updated and view permissions set in Google Drive folder! );
}
我也尝试了以下法典,但以下法典产生了腐败的国防军。
function updatePDF() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName( sheet_name ); // Target specific sheet
var folderId = my_google_drive_folder_id ;
var pdfName = filename.pdf ;
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFilesByName(pdfName);
var existingFile = files.hasNext() ? files.next() : null;
var url = https://docs.google.com/spreadsheets/d/ + spreadsheet.getId() + /export?exportFormat=pdf&format=pdf ;
var options = &size=letter +
&portrait=true +
&scale=2 +
&top_margin=0.25 + &bottom_margin=0.75 + &left_margin=0.25 + &right_margin=0.25 +
&sheetnames=false&printtitle=false&pagenumbers=true&gridlines=false&fzr=true +
&gid= + sheet.getSheetId(); // Using the specific Sheet ID for exporting
var token = ScriptApp.getOAuthToken();
var headers = {
Authorization : Bearer + token,
muteHttpExceptions : true
};
var response = UrlFetchApp.fetch(url + options, { headers: headers });
if (response.getResponseCode() == 200) {
var blob = response.getBlob().setName(pdfName);
// Create a new file and capture its ID
var newFile = folder.createFile(blob);
newFile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
// Delete the old file if it exists
if (existingFile) {
existingFile.setTrashed(true);
}
Logger.log( PDF updated successfully and permissions are set. );
} else {
Logger.log( Failed to fetch the PDF: + response.getContentText());
}
}