gpt4 book ai didi

excel - 将 vba 转换为 Office-JS

转载 作者:行者123 更新时间:2023-12-03 22:07:56 28 4
gpt4 key购买 nike

我目前正在考虑将一些遗留的 vba 代码转换为 Office-js,我只是想就现有代码是否可以轻松传输以及它是否适用于客户端和 Web 版本的 Excel 提供一些建议。

'''
Sub Notes()
Dim mySheet As Worksheet, myOtherSheet As Worksheet, myBook As Workbook 'Define your workbooks and worksheets as variables
Set myBook = Excel.ActiveWorkbook
Set mySheet = myBook.Sheets("Conversions")
Set myOtherSheet = myBook.Sheets("Additional Notes")

Dim i As Integer, j As Integer 'Define a couple integer variables for counting

j = 4 'This variable will keep track of which row we're on in Sheet2 (I'm assuming you want to start on line 28)
For i = 10 To 225 'This is the beginning the the loop which will repeat from 5 to 100 . . .
If mySheet.Cells(i, 17).Value <> "" Then ' . . . for each digit, it will check if the cell's value is blank. If it isn't then it will . . .
myOtherSheet.Cells(j, 2).Value = mySheet.Cells(i, 17).Value ' . . . Copy that value into the cell on Sheet2 in the row specified by our "j" variable.
j = j + 1 'Then we add one to the "j" variable so the next time it copies, we will be on the next available row in Sheet2.
End If
Next i 'This triggers the end of the loop and moves on to the next value of "i".
End Sub
Sub PDF_Export()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo ErrHandler

Set ws = ActiveSheet

'enter name and select folder for file
' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
& "_" _
& Format(Now(), "yyyymmdd\_hhmm") _
& ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile

myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")

If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

MsgBox "PDF file has been created."
End If

ExitHandler:
Exit Sub
ErrHandler:
MsgBox "Could not create PDF file"
Resume ExitHandler
End Sub

Sub Print_SummaryDocument()
'
' Print_Document Macro
'
Sheets("SFS v2.1.0").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True, Collate:=True, _
IgnorePrintAreas:=False

End Sub
Sub Print_DetailedDocument()
'
' Print_Document Macro
'
Sheets("Detailed Statement v2.1.0").Select
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
End Sub

Sub Print_Sum()

'Below statement will print 3 copy of the Sheet1 from Page 2 to Page no: 3

Worksheets("SFS v2.1.01").PrintOut From:=1, to:=3, Copies:=1, Preview:=True

End Sub

Sub PrintRange()

Range("A1:Ag257").PrintOut , Preview:=True

End Sub

> '''

如果那些比我知识更丰富的人可以确认代码是多么容易转移到 office-js,那就太好了(为

最佳答案

是的,您需要在 Office.JS 中重写您的代码。你可以引用这个文件:https://docs.microsoft.com/en-us/office/dev/add-ins/tutorials/excel-tutorial

从您的代码示例中,看起来您想导出 PDF 文件,在 office.JS 中您可以引用 getFileAsync,您可以从 https://docs.microsoft.com/en-us/javascript/api/office/office.document?view=office-js#getfileasync-filetype--callback- 获取有关此 API 使用的更多信息

PS。您也可以在 Script Lab 中尝试您的 Office JS 代码,https://www.myonlinetraininghub.com/script-lab-explore-the-office-javascript-api-learn-to-build-add-ins
https://docs.microsoft.com/en-us/office/dev/add-ins/overview/explore-with-script-lab

关于excel - 将 vba 转换为 Office-JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56188365/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com