如何修改Google Sheets脚本实现xlsx全量普通粘贴导入
Google Sheets 导入脚本修改方案
针对你提出的两个调整需求,修改逻辑如下:
- 取消固定数据范围:使用
getDataRange()方法自动识别源工作表内所有包含数据的连续行列,无需手动写死区域坐标 - 纯文本无格式写入:将读取到的所有单元格值统一转换为普通字符串,写入前清空目标区域的格式设置,彻底避免带入加粗、大字号等源格式/触发目标表残留格式规则
修改后的完整代码:
// 自动读取源表全量数据,统一转为普通文本格式 const sourceRange = sourceSheet.getDataRange(); const sourceValues = sourceRange.getValues().map(row => row.map(cell => cell === null || cell === undefined ? '' : String(cell)) ); const target = SpreadsheetApp.openById(ID).getSheetByName('sheetname'); const insertStartRow = target.getLastRow() + 1; const targetWriteRange = target.getRange(insertStartRow, 1, sourceValues.length, sourceValues[0].length); // 清空待写入区域格式后写入纯文本内容 targetWriteRange.clearFormat(); targetWriteRange.setValues(sourceValues);
补充说明:如果你仍需要保留原逻辑从B列开始读取源表数据(跳过A列),只需要替换源范围获取部分的代码即可:
// 从第1行、第2列(B列)开始,读取到源表最后一行、最后一列的所有数据 const lastRow = sourceSheet.getLastRow(); const lastCol = sourceSheet.getLastColumn(); const sourceRange = sourceSheet.getRange(1, 2, lastRow, lastCol - 1);
内容的提问来源于stack exchange,提问作者rodskies
相关产品推荐
相关产品推荐

