Google 电子表格范围的屏幕截图
screenshot of Google spreadsheet range
每当值在 Google 电子表格中发生变化时,我都会尝试截取该范围内的单元格的屏幕截图,并将屏幕截图保存在 URL 或我的驱动器中作为 . jpg、.png 或 pdf
我能够找到类似的东西来截取图表的屏幕截图,但无法针对这种情况成功修改脚本,有没有人做过类似的东西
以此为参考,如需修改:
代码:
function exportPdf(e) {
var spreadsheet = e.source;
var sheet = spreadsheet.getActiveSheet();
var range = e.range;
// sample dimension to be checked is B2:E5
var checkDimension = {
startingRow: 2,
startingColumn: 2,
endingRow: 5,
endingColumn: 5
};
// range should be within checkDimension and Sheet1
if(sheet.getSheetName() == 'Sheet1' &&
range.getRow() >= checkDimension.startingRow &&
range.getLastRow() <= checkDimension.endingRow &&
range.getColumn() >= checkDimension.startingColumn &&
range.getLastColumn() <= checkDimension.endingColumn) {
var spreadsheetId = spreadsheet.getId();
var sheetId = sheet.getSheetId();
var exportRange = "B2:E5";
var urlString = 'export?exportFormat=pdf&format=pdf' +
'&gid=' + sheetId + '&id=' + spreadsheetId +
'&range=' + exportRange +
'&size=A4' + // paper size
'&portrait=true'; // orientation, false for landscape
// See https://webapps.stackexchange.com/questions/130654/all-google-docs-url-parameters-functions-commands for more url parameters
var exportUrl = spreadsheet.getUrl().replace(/edit.*$/, urlString);
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
},
muteHttpExceptions: true
}
var response = UrlFetchApp.fetch(exportUrl, options);
if (response.getResponseCode() !== 200) {
Logger.log("Error exporting Sheet to PDF! Response Code: " + response.getResponseCode());
return;
}
var blob = response.getBlob();
var timestamp = new Date().toISOString();
// set name to spreadsheet_sheet_range_timestamp.pdf
blob.setName(spreadsheet.getName() + '_' + sheet.getSheetName() + '_' + exportRange + '_' + timestamp + '.pdf');
var folderId = 'enter your folder ID here';
// Create the PDF file in the specific folder
DriveApp.getFolderById(folderId).createFile(blob);
}
}
注:
- 有关更多 url 参数,请参阅下面的参考资料。
- 这需要是一个可安装的触发器,而不是一个简单的触发器。
输出:
参考文献:
- Url parameters
- Alan Well's answer
每当值在 Google 电子表格中发生变化时,我都会尝试截取该范围内的单元格的屏幕截图,并将屏幕截图保存在 URL 或我的驱动器中作为 . jpg、.png 或 pdf 我能够找到类似的东西来截取图表的屏幕截图,但无法针对这种情况成功修改脚本,有没有人做过类似的东西
以此为参考,如需修改:
代码:
function exportPdf(e) {
var spreadsheet = e.source;
var sheet = spreadsheet.getActiveSheet();
var range = e.range;
// sample dimension to be checked is B2:E5
var checkDimension = {
startingRow: 2,
startingColumn: 2,
endingRow: 5,
endingColumn: 5
};
// range should be within checkDimension and Sheet1
if(sheet.getSheetName() == 'Sheet1' &&
range.getRow() >= checkDimension.startingRow &&
range.getLastRow() <= checkDimension.endingRow &&
range.getColumn() >= checkDimension.startingColumn &&
range.getLastColumn() <= checkDimension.endingColumn) {
var spreadsheetId = spreadsheet.getId();
var sheetId = sheet.getSheetId();
var exportRange = "B2:E5";
var urlString = 'export?exportFormat=pdf&format=pdf' +
'&gid=' + sheetId + '&id=' + spreadsheetId +
'&range=' + exportRange +
'&size=A4' + // paper size
'&portrait=true'; // orientation, false for landscape
// See https://webapps.stackexchange.com/questions/130654/all-google-docs-url-parameters-functions-commands for more url parameters
var exportUrl = spreadsheet.getUrl().replace(/edit.*$/, urlString);
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
},
muteHttpExceptions: true
}
var response = UrlFetchApp.fetch(exportUrl, options);
if (response.getResponseCode() !== 200) {
Logger.log("Error exporting Sheet to PDF! Response Code: " + response.getResponseCode());
return;
}
var blob = response.getBlob();
var timestamp = new Date().toISOString();
// set name to spreadsheet_sheet_range_timestamp.pdf
blob.setName(spreadsheet.getName() + '_' + sheet.getSheetName() + '_' + exportRange + '_' + timestamp + '.pdf');
var folderId = 'enter your folder ID here';
// Create the PDF file in the specific folder
DriveApp.getFolderById(folderId).createFile(blob);
}
}
注:
- 有关更多 url 参数,请参阅下面的参考资料。
- 这需要是一个可安装的触发器,而不是一个简单的触发器。
输出:
参考文献:
- Url parameters
- Alan Well's answer